package com.its.pis.ui;

import com.its.pis.entity.TbVdsCtlr;
import com.its.pis.entity.TbVdsCtlrStts;
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<TbVdsCtlr> ctlrList = Collections.synchronizedList(new ArrayList<TbVdsCtlr>());
    private final String[] columnNames = {
            "#",
            "번호",
            "시설물ID",
            "명칭",
            "IP",
            "PORT",
            "연결상태",
            "도어",
            "팬",
            "히터",
            "온도",
            "영상",
            "연결 시각",
            "연결종료 시각"
    };
    public static final String[] netStateStr = {
            "Close", "Login", "Connect",
    };

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

        int indexCount = 1;
        for (TbVdsCtlr 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 TbVdsCtlr getControllerInfo(int row) {
        TbVdsCtlr info = this.ctlrList.get(row);
        return info;
    }

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

            int netState = info.getNetState();
            TbVdsCtlrStts stts = info.getStts();
            String door = "-";
            String fan = "-";
            String heater = "-";
            String temper = "-";
            String video = "-";
            if ("CMS0".equals(stts.getCMNC_STTS_CD())) {
                door = "-?-";
                fan = "-?-";
                heater = "-?-";
                temper = "-?-";
                video = "-?-";
                if (stts.getCBOX_DOOR_STTS_CD().equals("CDS0")) {
                    door = "닫힘";
                } else if (stts.getCBOX_DOOR_STTS_CD().equals("CDS1")) {
                    door = "열림";
                }
                if (stts.getFAN_STTS_CD().equals("PAS0")) {
                    fan = "가동";
                } else if (stts.getFAN_STTS_CD().equals("PAS1")) {
                    fan = "중지";
                }
                if (stts.getHETR_STTS_CD().equals("HTS0")) {
                    heater = "가동";
                } else if (stts.getHETR_STTS_CD().equals("HTS1")) {
                    heater = "중지";
                }
                if (stts.getVIDEO_INPUT().equals("VDI0")) {
                    video = "정상";
                } else if (stts.getHETR_STTS_CD().equals("VDI1")) {
                    video = "이상";
                }
                temper = String.valueOf(stts.getCBOX_TMPR());
            }
            switch (columnIndex) {
                case 0:
                    returnValue = info.getIndex();
                    break;
                case 1:
                    returnValue = info.getVDS_CTLR_NMBR();
                    break;
                case 2:
                    returnValue = info.getVDS_CTLR_ID();
                    break;
                case 3:
                    returnValue = info.getVDS_NM();
                    break;
                case 4:
                    returnValue = info.getVDS_CTLR_IP();
                    break;
                case 5:
                    returnValue = String.valueOf(info.getVDS_CTLR_PORT());
                    break;
                case 6:
                    returnValue = netStateStr[netState];
                    break;
                case  7: returnValue = door; break;
                case  8: returnValue = fan; break;
                case  9: returnValue = heater; break;
                case 10: returnValue = temper; break;
                case 11: returnValue = video; 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) {
            TbVdsCtlr obj = ctlrList.get(rowIndex);
            if (columnIndex == 0) {
                obj.setIndex((int) value);
            }
        }
    }
    public void setValueAt(TbVdsCtlr obj, int rowIdx, int colIdx) {
        synchronized (this.ctlrList) {
        }
        fireTableCellUpdated(rowIdx, colIdx);
        fireTableDataChanged();
    }

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

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

}
