package com.its.pis.ui;

import com.its.pis.entity.TbPisInfr;
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<TbPisInfr> ctlrList = Collections.synchronizedList(new ArrayList<TbPisInfr>());
    private final String[] columnNames = {
            "#",
            "번호",
            "주차장 ID",
            "명칭",
            "IP",
            "PORT",
            "연결상태",
            "유형",
            "주기",
            "END-POINT",
            "TOKEN",
            "연결 시각",
            "연결종료 시각",
            "정보수신 시각"
    };
    public static final String[] netStateStr = {
            "Close", "Login", "Connect",
    };

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

        int indexCount = 1;
        for (TbPisInfr obj : ctlrList) {
            obj.setIndex(indexCount++);
        }
        if (ctlrList.size() == 0) {
            log.error("CtrlSttsTableModel Data zero: {}", 0);
        }
    }

    @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 TbPisInfr getControllerInfo(int row) {
        TbPisInfr info = this.ctlrList.get(row);
        return info;
    }

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

            int netState = info.getNetState();
            switch (columnIndex) {
                case 0:
                    returnValue = info.getIndex();
                    break;
                case 1:
                    returnValue = info.getPIS_NMBR();
                    break;
                case 2:
                    returnValue = info.getPIS_ID();
                    break;
                case 3:
                    returnValue = info.getPIS_NM();
                    break;
                case 4:
                    returnValue = info.getPIS_IP();
                    break;
                case 5:
                    returnValue = String.valueOf(info.getPIS_PORT());
                    break;
                case 6:
                    returnValue = netStateStr[netState];
                    break;
                case  7: returnValue = info.getPIS_TP(); break;
                case  8: returnValue = String.valueOf(info.getPIS_CYCLE()); break;
                case  9: returnValue = info.getPIS_END_POINT(); break;
                case 10: returnValue = info.getPIS_TOKEN(); break;
                case 11:
                    returnValue = info.getConnectTm();
                    break;
                case 12:
                    returnValue = info.getDisConnectTm();
                    break;
                case 13:
                    returnValue = info.getLastRecvTimeFmt();
                    break;
            }
        }
        return returnValue;
    }

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

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

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

}
