package com.tsi.navi.activity.fragment;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.tsi.navi.R;
import com.tsi.navi.TsiNaviApplication;
import com.tsi.navi.activity.model.SignalItem;
import com.tsi.navi.activity.model.CpuControl;
import com.tsi.navi.dto.CpuNodeStatusDTO;
import com.tsi.navi.dto.enums.eLightsStatus;
import com.tsi.navi.dto.enums.eLightsType;
import com.tsi.navi.util.TsiUtils;
import com.tsi.navi.vo.NodeVo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DirectionSignalFragment extends Fragment {
    private static final String TAG = DirectionSignalFragment.class.getSimpleName();

    private Context context;
    //private Spinner spinner;

    private NodeVo node;
    private long nodeId;
    private CpuControl cpuControl;

    private TextView tvName;
    private TextView tvCycle;
    private Button btnDirection;

    private ImageView[] imgSignal = new ImageView[TsiUtils.MAX_LIGHT_TYPE];
    private TextView[] tvSignal = new TextView[TsiUtils.MAX_LIGHT_TYPE];
    private TextView[] tvStatus = new TextView[CpuControl.MAX_STATUS];

    private int directionCode;
    private boolean first;
    private Set<String> directions = new HashSet();

    private int normalTextColor;
    private int activeTextColor;

    private int[] drawId = {
            R.drawable.rect_off,            // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
            R.drawable.rect_red,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
            R.drawable.rect_yellow_blink,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
            R.drawable.rect_green,          // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
            R.drawable.rect_red,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
            R.drawable.rect_yellow_blink,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
            R.drawable.rect_green,          // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
            R.drawable.rect_none,           // 데이터 없음.
    };
    private int[] drawIdNobord = {
            R.drawable.rect_off_nobord,     // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
            R.drawable.rect_red_nobord,     // RED_SOLID    (1, "RED_SOLID"),      //적색점등
            R.drawable.rect_yellow_blink_nobord,  // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
            R.drawable.rect_green_nobord,   // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
            R.drawable.rect_red_nobord,     // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
            R.drawable.rect_yellow_blink_nobord,  // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
            R.drawable.rect_green_nobord,   // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
            R.drawable.rect_none,           // 데이터 없음.
    };
    private int[][] signalId = {
            {   // none
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
            },
            {   // 직진
                    R.drawable.ic_signal_straight_off,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_straight_red,      // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_straight_yellow,   // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_straight_green,    // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_straight_red,      // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_straight_yellow,   // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_straight_green,    // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 좌회전
                    R.drawable.ic_signal_left_off,          // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_left_red,          // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_left_yellow,       // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_left_green,        // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_left_red,          // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_left_yellow,       // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_left_green,        // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 보행자
                    R.drawable.ic_signal_walk_off,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_walk_red,        // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_walk_yellow,     // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_walk_green,         // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_walk_red,        // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_walk_yellow,     // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_walk_green,         // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 자전거
                    R.drawable.ic_signal_off,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_red,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_yellow,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_green,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_red,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_yellow,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_green,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 우회전
                    R.drawable.ic_signal_off,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_red,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_yellow,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_green,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_red,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_yellow,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_green,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 버스
                    R.drawable.ic_signal_off,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_red,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_yellow,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_green,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_red,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_yellow,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_green,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 유턴
                    R.drawable.ic_signal_uturn_off,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_uturn_red,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_uturn_yellow,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_uturn_green,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_uturn_red,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_uturn_yellow,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_uturn_green,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
    };
    private int[][] signalIdUn = {
            {   // none
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
                    R.drawable.rect_none,                  // 데이터 없음.
            },
            {   // 직진
                    R.drawable.ic_signal_straight_off_un,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_straight_red_un,      // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_straight_yellow_un,   // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_straight_green_un,    // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_straight_red_un,      // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_straight_yellow_un,   // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_straight_green_un,    // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 좌회전
                    R.drawable.ic_signal_left_off_un,          // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_left_red_un,          // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_left_yellow_un,       // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_left_green_un,        // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_left_red_un,          // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_left_yellow_un,       // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_left_green_un,        // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 보행자
                    R.drawable.ic_signal_walk_off_un,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_walk_red_un,        // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_walk_yellow_un,     // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_walk_green_un,         // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_walk_red_un,        // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_walk_yellow_un,     // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_walk_green_un,         // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 자전거
                    R.drawable.ic_signal_off_un,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_red_un,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_yellow_un,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_green_un,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_red_un,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_yellow_un,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_green_un,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 우회전
                    R.drawable.ic_signal_off_un,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_red_un,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_yellow_un,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_green_un,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_red_un,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_yellow_un,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_green_un,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 버스
                    R.drawable.ic_signal_off_un,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_red_un,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_yellow_un,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_green_un,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_red_un,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_yellow_un,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_green_un,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
            {   // 유턴
                    R.drawable.ic_signal_uturn_off_un,      // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
                    R.drawable.ic_signal_uturn_red_un,            // RED_SOLID    (1, "RED_SOLID"),      //적색점등
                    R.drawable.ic_signal_uturn_yellow_un,         // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
                    R.drawable.ic_signal_uturn_green_un,       // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
                    R.drawable.ic_signal_uturn_red_un,            // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
                    R.drawable.ic_signal_uturn_yellow_un,         // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
                    R.drawable.ic_signal_uturn_green_un,       // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
                    R.drawable.rect_none,                   // 데이터 없음.
            },
    };

    private int[] textColor = {
            Color.WHITE,        // LIGHTS_OUT   (0, "LIGHTS_OUT"),     //소등
            Color.WHITE,        // RED_SOLID    (1, "RED_SOLID"),      //적색점등
            Color.WHITE,        // YELLOW_SOLID (2, "YELLOW_SOLID"),   //황색점등
            Color.WHITE,        // GREEN_SOLID  (3, "GREEN_SOLID"),    //녹색점등
            Color.WHITE,        // RED_BLINK    (4, "RED_BLINK"),      //적색점멸
            Color.WHITE,        // YELLOW_BLINK (5, "YELLOW_BLINK"),   //황색점멸
            Color.WHITE,        // GREEN_BLINK  (6, "GREEN_BLINK");    //녹색점멸
            Color.WHITE,        // 데이터 없음.
    };

    private SignalItem signalCurr = null;
    private SignalItem signalPrev = null;

    // 소등(0), 적색점등(1), 황색점등(2), 녹색점등(3), 적색점멸(4), 황색점멸(5), 녹색점멸(6)
    // 미지정(0), 직진(1), 좌회전(2), 보행자(3), 자전거(4), 우회전(5), 버스(6), 유턴(7)

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_direction_signal, container, false);

        //this.spinner = v.findViewById(R.id.sp_direction);

        this.tvName = (TextView)v.findViewById(R.id.tv_name);
        this.tvCycle = v.findViewById(R.id.tv_cycle);
        this.btnDirection = (Button) v.findViewById(R.id.btn_direction);
        this.btnDirection.setOnClickListener(view -> {
            if (this.directions.size() == 0) {
                return;
            }
            Log.e("Direction", this.directions.toString());
            List<String> directionList = new ArrayList(this.directions);
            Collections.sort(directionList);
            String[] dirs = new String[directionList.size()];
            for(int ii = 0; ii  < directionList.size(); ii++) {
                dirs[ii] = TsiNaviApplication.getDirection().getName(Integer.parseInt(directionList.get(ii)));
            }
            Log.e("Direction", dirs.toString());

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("방향코드 선택");
            builder.setItems(dirs, (dialog, which) -> {
                btnDirection.setText(dirs[which]);
                int selDirection = TsiNaviApplication.getDirection().getNameCode(dirs[which]);
                if (selDirection != directionCode) {
                    initStatus();
                    setDirectionCode(selDirection);
                }
                }
            );
            builder.show();
        });

        this.tvStatus[0] = v.findViewById(R.id.tv_status1); // 전이
        this.tvStatus[1] = v.findViewById(R.id.tv_status2); // 감응
        this.tvStatus[2] = v.findViewById(R.id.tv_status3); // 소등
        this.tvStatus[3] = v.findViewById(R.id.tv_status4); // 점멸
        this.tvStatus[4] = v.findViewById(R.id.tv_status5); // 수동

        // 미지정(0), 직진(1), 좌회전(2), 보행자(3), 자전거(4), 우회전(5), 버스(6), 유턴(7)
        this.tvSignal[0]                                 = (TextView) null;
        this.tvSignal[eLightsType.STRAIGHT.getValue()]   = (TextView) v.findViewById(R.id.tv_straight);
        this.tvSignal[eLightsType.LEFT_TURN.getValue()]  = (TextView) v.findViewById(R.id.tv_left_turn);
        this.tvSignal[eLightsType.PEDESTRIAN.getValue()] = (TextView) v.findViewById(R.id.tv_walker);
        this.tvSignal[eLightsType.BICYCLE.getValue()]    = (TextView) v.findViewById(R.id.tv_bicycle);
        this.tvSignal[eLightsType.RIGHT_TURN.getValue()] = (TextView) v.findViewById(R.id.tv_right_turn);
        this.tvSignal[eLightsType.BUS.getValue()]        = (TextView) v.findViewById(R.id.tv_bus);
        this.tvSignal[eLightsType.U_TURN.getValue()]     = (TextView) v.findViewById(R.id.tv_u_turn);

        this.imgSignal[0]                                 = (ImageView) null;
        this.imgSignal[eLightsType.STRAIGHT.getValue()]   = (ImageView) v.findViewById(R.id.img_straight);
        this.imgSignal[eLightsType.LEFT_TURN.getValue()]  = (ImageView) v.findViewById(R.id.img_left_turn);
        this.imgSignal[eLightsType.PEDESTRIAN.getValue()] = (ImageView) v.findViewById(R.id.img_walker);
        this.imgSignal[eLightsType.BICYCLE.getValue()]    = (ImageView) v.findViewById(R.id.img_bicycle);
        this.imgSignal[eLightsType.RIGHT_TURN.getValue()] = (ImageView) v.findViewById(R.id.img_right_turn);
        this.imgSignal[eLightsType.BUS.getValue()]        = (ImageView) v.findViewById(R.id.img_bus);
        this.imgSignal[eLightsType.U_TURN.getValue()]     = (ImageView) v.findViewById(R.id.img_u_turn);

        this.normalTextColor = ContextCompat.getColor(this.context, R.color.rect_normal_color);
        this.activeTextColor = ContextCompat.getColor(this.context, R.color.rect_active_color);

        this.first = false;
        this.directionCode = 0;

        if (this.signalCurr == null) {
            this.signalCurr = new SignalItem();
        }
        if (this.signalPrev == null) {
            this.signalPrev = new SignalItem();
        }

        this.cpuControl = new CpuControl();
        this.nodeId = 0;

        init(0);

        return v;
    }

    protected void setDirectionCode(int directionCode) {
        this.directionCode = directionCode;
        String name = TsiNaviApplication.getDirection().getName(this.directionCode);
        this.btnDirection.setText(name);
        this.signalCurr.setDirection(this.directionCode);
        this.signalPrev.setDirection(this.directionCode);
    }

    protected void init(int directionCode) {
        if (this.node != null) {
            this.tvName.setText(this.node.getName());
            this.nodeId = this.node.getNodeId();
        }
        else {
            this.tvName.setText("-");
            this.nodeId = 0;
        }

        this.tvCycle.setText("-");

        initStatus();
        setDirectionCode(directionCode);
    }
    @SuppressLint("ObsoleteSdkInt")
    public void initStatus() {
        for (int ii = 0; ii < CpuControl.MAX_STATUS; ii++) {
            if (this.tvStatus[ii].getAnimation() != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    this.tvStatus[ii].getAnimation().cancel();
                }
                this.tvStatus[ii].clearAnimation();
                this.tvStatus[ii].setAnimation(null);
            }
            if (ii == 3) {
                this.tvStatus[ii].setBackgroundResource(R.drawable.rect_border);  // 점멸
            }
            else {
                this.tvStatus[ii].setBackgroundResource(R.drawable.rect_none);    // 수동
            }
            this.tvStatus[ii].setTextColor(this.normalTextColor);
        }
        this.cpuControl.init();

        for (int ii = TsiUtils.SIGNAL_STRAIGHT; ii <= TsiUtils.SIGNAL_U_TURN; ii++) {
            if (this.imgSignal[ii] != null) {
                if (this.imgSignal[ii].getAnimation() != null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        this.imgSignal[ii].getAnimation().cancel();
                    }
                    this.imgSignal[ii].clearAnimation();
                    this.imgSignal[ii].setAnimation(null);
                }

                if (ii == TsiUtils.SIGNAL_STRAIGHT) {
                    this.imgSignal[ii].setImageResource(R.drawable.rect_border);
                }
                else {
                    this.imgSignal[ii].setImageResource(R.drawable.rect_border_left);
                }
            }

            if (this.tvSignal[ii] != null) {
                if (ii == TsiUtils.SIGNAL_STRAIGHT) {
                    this.tvSignal[ii].setBackgroundResource(R.drawable.rect_border);
                }
                else {
                    this.tvSignal[ii].setBackgroundResource(R.drawable.rect_border_left);
                }
                this.tvSignal[ii].setTextColor(this.normalTextColor);
                this.tvSignal[ii].setText("");
            }
        }

        this.signalCurr.initStatus();
        this.signalPrev.initStatus();
    }
    public long getNodeId() {
        return this.nodeId;
    }

    public void setNodeCross(NodeVo node, int directionCode) {
        this.first = true;
        this.node = node;
        init(directionCode);
    }

    @SuppressLint("ObsoleteSdkInt")
    public void updateSignal(CpuNodeStatusDTO status) {
        if (this.node == null || this.nodeId != status.getA()) {
            return;
        }

        if (this.first) {
            this.directions.clear();
            int directionCode = this.directionCode;
            for (int ii = 0; ii < status.getG().size(); ii++) {
                this.directions.add(String.valueOf(status.getG().get(ii).getH()));
                if (ii == 0) {
                    directionCode = status.getG().get(ii).getH();
                }
                if (this.directionCode == status.getG().get(ii).getH()) {
                    directionCode = this.directionCode;
                }
            }
            // 프로그램에서 설정한 방향코드가 수집한 상태정보의 방향코드에 존재하지 않으면 첫번째 방향코드로 변경한다.
            if (this.directionCode != directionCode) {
                setDirectionCode(directionCode);
            }

            this.first = false;
        }

        this.tvCycle.setText(String.valueOf(status.getC()));

        this.cpuControl.updateStatus(status.getD());
        if (this.cpuControl.isUpdate) {
            for (int ii = 0; ii < CpuControl.MAX_STATUS; ii++) {

                this.tvStatus[ii].setTextColor(this.cpuControl.status[ii] ? this.activeTextColor : this.normalTextColor);

                if (this.tvStatus[ii].getAnimation() != null) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        this.tvStatus[ii].getAnimation().cancel();
                    }
                    this.tvStatus[ii].clearAnimation();
                    this.tvStatus[ii].setAnimation(null);
                }
                if (ii == 3) {
                    this.tvStatus[ii].setBackgroundResource(this.cpuControl.status[ii] ? R.drawable.rect_active : R.drawable.rect_border);  // 점멸
                }
                else {
                    this.tvStatus[ii].setBackgroundResource(this.cpuControl.status[ii] ? R.drawable.rect_active : R.drawable.rect_none);
                }
                if (this.cpuControl.status[ii]) {
                    Animation animation = AnimationUtils.loadAnimation(this.context, R.anim.blink);
                    this.tvStatus[ii].startAnimation(animation);
                }
            }
        }

        // 방향코드에 맞는 데이터 표출
        for (int ii = 0; ii < status.getG().size(); ii++) {
            if (status.getG().get(ii).getH() == this.directionCode) {
                this.signalCurr.setStatus(status.getG().get(ii));
            }
        }

        int lightStatus;
        int remainSeconds;
        boolean unProtected;
        for (int ii = TsiUtils.SIGNAL_STRAIGHT; ii <= TsiUtils.SIGNAL_U_TURN; ii++) {
            lightStatus = this.signalCurr.getLightStatus(ii);
            remainSeconds = this.signalCurr.getRemainTime(ii);
            unProtected = this.signalCurr.getUnprotect(ii);
            if (lightStatus >= eLightsStatus.LIGHTS_OUT.getValue() && lightStatus <= eLightsStatus.GREEN_BLINK.getValue()) {

                this.tvSignal[ii].setText(String.valueOf(remainSeconds));

                if (lightStatus != signalPrev.getLightStatus(ii)) {
                    if (signalPrev.getLightStatus(ii) >= eLightsStatus.RED_BLINK.getValue()) {
                        if (this.imgSignal[ii].getAnimation() != null) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                                this.imgSignal[ii].getAnimation().cancel();
                            }
                            this.imgSignal[ii].clearAnimation();
                            this.imgSignal[ii].setAnimation(null);
                        }
                    }
                    //this.tvSignal[ii].setBackgroundResource((ii == TsiUtils.SIGNAL_STRAIGHT) ? drawIdNobord[lightStatus] : drawId[lightStatus]);
                    //this.tvSignal[ii].setTextColor(textColor[lightStatus]);

                    this.imgSignal[ii].setImageResource(unProtected ? signalIdUn[ii][lightStatus] : signalId[ii][lightStatus]);
                    if (lightStatus >= eLightsStatus.RED_BLINK.getValue()) {
                        Animation animation = AnimationUtils.loadAnimation(this.context, R.anim.blink);
                        this.imgSignal[ii].startAnimation(animation);
                    }
                }
            }
            else {
                //this.tvSignal[ii].setBackgroundResource((ii == TsiUtils.SIGNAL_STRAIGHT) ? R.drawable.rect_border : R.drawable.rect_border_left);
                this.tvSignal[ii].setText("");
            }
            signalPrev.setLightStatus(ii, lightStatus);
        }
    }
}
