package com.its.dsrc.ui;

import com.its.dsrc.entity.TbRseCtlr;
import com.its.dsrc.entity.TbRseCtlrStts;
import lombok.extern.slf4j.Slf4j;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
public class CtlrSttsTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1331132425472559704L;

    private List<TbRseCtlr> ctlrList = Collections.synchronizedList(new ArrayList<TbRseCtlr>());
    private final String[] columnNames = {
            "#",
            "번호",
            "RSE ID",
            "명칭",
            "IP",
            "PORT",
            "연결상태",
            "장치상태",
            "ATN-1",
            "ATN-2",
            "ATN-3",
            "ATN-4",
            "연결 시각",
            "연결종료 시각"
    };
    public static final String[] netStateStr = {
            "Close", "Login", "Connect",
    };

    public CtlrSttsTableModel(List<TbRseCtlr> ctlrList) {
        this.ctlrList = ctlrList;

        int indexCount = 1;
        for (TbRseCtlr obj : ctlrList) {
            obj.setIndex(indexCount++);
        }
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public int getRowCount() {
        int size = 0;
        synchronized (this.ctlrList) {
            size = this.ctlrList.size();
        }
        return size;
    }

    @Override
    public String getColumnName(int columnIndex) {
        if (columnIndex < columnNames.length) {
            return columnNames[columnIndex];
        }
        return super.getColumnName(columnIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (ctlrList.isEmpty()) {
            return Object.class;
        }
        return getValueAt(0, columnIndex).getClass();
    }

    public TbRseCtlr getControllerInfo(int row) {
        TbRseCtlr info = this.ctlrList.get(row);
        return info;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object returnValue = null;
        synchronized (this.ctlrList) {
            TbRseCtlr info = this.ctlrList.get(rowIndex);
            if (info == null) {
                return "";
            }

            int netState = info.getNetState();
            TbRseCtlrStts stts = info.getStts();
            String devStts = "-";
            String antena1 = "-";
            String antena2 = "-";
            String antena3 = "-";
            String antena4 = "-";
            if ("CMS0".equals(stts.getCMNC_STTS_CD())) {
               switch(Integer.parseInt(stts.getCNTL_DEVC_STTS())) {
                   case 0: devStts = "정상"; break;
                   case 1: devStts = "문열림"; break;
                   case 2: devStts = "팬동작"; break;
                   case 3: devStts = "온도이상"; break;
                   case 4: devStts = "정보없음"; break;
                   default: devStts = stts.getCNTL_DEVC_STTS(); break;
                }
                antena1 = stts.getATN_1_STTS();
                antena2 = stts.getATN_2_STTS();
                antena3 = stts.getATN_3_STTS();
                antena4 = stts.getATN_4_STTS();
            }
            switch (columnIndex) {
                case 0:
                    returnValue = info.getIndex();
                    break;
                case 1:
                    returnValue = info.getID();
                    break;
                case 2:
                    returnValue = info.getRSE_ID();
                    break;
                case 3:
                    returnValue = " " + info.getRSE_NM();
                    break;
                case 4:
                    returnValue = info.getRSE_CTLR_IP();
                    break;
                case 5:
                    returnValue = String.valueOf(info.getRSE_CTLR_PORT());
                    break;
                case 6:
                    returnValue = netStateStr[netState];
                    break;
                case  7: returnValue = devStts; break;
                case  8: returnValue = antena1; break;
                case  9: returnValue = antena2; break;
                case 10: returnValue = antena3; break;
                case 11: returnValue = antena4; break;
                case 12:
                    returnValue = info.getConnectTm();
                    break;
                case 13:
                    returnValue = info.getDisConnectTm();
                    break;
            }
        }
        return returnValue;
    }

    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        synchronized (this.ctlrList) {
            TbRseCtlr obj = ctlrList.get(rowIndex);
            if (columnIndex == 0) {
                obj.setIndex((int) value);
            }
        }
    }
    public void setValueAt(TbRseCtlr obj, int rowIdx, int colIdx) {
        synchronized (this.ctlrList) {
        }
        fireTableCellUpdated(rowIdx, colIdx);
        fireTableDataChanged();
    }

    public void Add(TbRseCtlr info) {
        int index = 0;
        synchronized (this.ctlrList) {
            index = this.ctlrList.size();
            this.ctlrList.add(info);
        }
        fireTableRowsInserted(index, index);
    }

    public void setValue(TbRseCtlr obj, int viewRow, int modelRow) {
        fireTableDataChanged();
    }

}
