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 = {
            "#",
            "번호",
            "MCU ID",
            "명칭",
            "IP",
            "PORT",
            "연결상태",
            "온도",
            "앞문",
            "뒷문",
            "팬",
            "히터",
            "연결 시각",
            "연결종료 시각"
    };
    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 temper = "-";
            String fDoor = "-";
            String bDoor = "-";
            String fan = "-";
            String hetr = "-";
            String rtu = "-";
            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;
//                }
                temper = String.valueOf(stts.getCBOX_TMPR());
                if ("CDS0".equals(stts.getFRONT_DOOR_STTS_CD())) {
                    fDoor = "닫힘";
                } else if ("CDS1".equals(stts.getFRONT_DOOR_STTS_CD())) {
                    fDoor = "열림";
                }
                if ("CDS0".equals(stts.getBACK_DOOR_STTS_CD())) {
                    bDoor = "닫힘";
                } else if ("CDS1".equals(stts.getBACK_DOOR_STTS_CD())) {
                    bDoor = "열림";
                }
                if ("PAS0".equals(stts.getFAN_STTS_CD())) {
                    fan = "가동";
                } else if ("PAS1".equals(stts.getFAN_STTS_CD())) {
                    fan = "중지";
                }
                if ("HTS0".equals(stts.getHETR_STTS_CD())) {
                    hetr = "가동";
                } else if ("HTS1".equals(stts.getHETR_STTS_CD())) {
                    hetr = "중지";
                }
                if ("RTU1".equals(stts.getRTU_STTS_CD())) {
                    rtu = "정상";
                } else if ("VDI1".equals(stts.getRTU_STTS_CD())) {
                    rtu = "이상";
                }
            }
            switch (columnIndex) {
                case 0:
                    returnValue = info.getIndex();
                    break;
                case 1:
                    returnValue = info.getID();
                    break;
                case 2:
                    returnValue = info.getRSE_MCU_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 = temper; break;
                case  8: returnValue = fDoor; break;
                case  9: returnValue = bDoor; break;
                case 10: returnValue = fan; break;
                case 11: returnValue = hetr; 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();
    }

}
