package com.its.vms.ui;

import com.its.vms.dto.TbVmsCtlrDto;
import com.its.vms.entity.TbVmsCtlrStts;
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<TbVmsCtlrDto> ctlrList = Collections.synchronizedList(new ArrayList<TbVmsCtlrDto>());
    private final String[] columnNames = {
            "#",
            "NO",
            "ID",
            "Name",
            "IP Address",
            "Mode",
            "연결상태",
            "Connect",
            "Disconnect",
            "Connected"
    };
    public static final String[] netStateStr = {
            "Close", "Login", "Connect",
    };

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

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

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

            int netState = info.getNetState();
            TbVmsCtlrStts stts = info.getStts();
            String temper = "-";
            String fDoor = "-";
            String bDoor = "-";
            String fan = "-";
            String hetr = "-";
            String rtu = "-";
            if ("CMS0".equals(stts.getCmncSttsCd())) {
                temper = String.valueOf(stts.getCboxTmpr());
                if ("CDS0".equals(stts.getCboxDoorSttsCd())) {
                    fDoor = "닫힘";
                } else if ("CDS1".equals(stts.getCboxDoorSttsCd())) {
                    fDoor = "열림";
                } else {
                    fDoor = "-?-";
                }

                if ("CDS0".equals(stts.getCboxDoorSttsCd())) {
                    bDoor = "닫힘";
                } else if ("CDS1".equals(stts.getCboxDoorSttsCd())) {
                    bDoor = "열림";
                } else {
                    bDoor = "-?-";
                }

                if ("PAS0".equals(stts.getFanSttsCd())) {
                    fan = "가동";
                } else if ("PAS1".equals(stts.getFanSttsCd())) {
                    fan = "중지";
                } else {
                    fan = "-?-";
                }
                if ("HTS0".equals(stts.getHetrSttsCd())) {
                    hetr = "가동";
                } else if ("HTS1".equals(stts.getHetrSttsCd())) {
                    hetr = "중지";
                } else {
                    hetr = "-?-";
                }
            }
            switch (columnIndex) {
                case 0: returnValue = info.getIndex(); break;
                case 1: returnValue = info.getVmsCtlrNmbr(); break;
                case 2: returnValue = info.getCtlrId(); break;
                case 3: returnValue = " " + info.getName(); break;
                case 4: returnValue = info.getCtlrIp(); break;
                case 5: returnValue = info.getOperMode(); break;
                case 6: returnValue = netStateStr[netState]; break;
                case  7: returnValue = info.getConnectTm(); break;
                case  8: returnValue = info.getDisConnectTm(); break;
                case  9: returnValue = info.getConnectCount(); break;
            }
        }
        return returnValue;
    }

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

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

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

}
