package com.its.wthr.ui;

import com.its.wthr.vo.AtmpPltnVo;
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 AtmpTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1331132425472559704L;

    private List<AtmpPltnVo> ctlrList = Collections.synchronizedList(new ArrayList<AtmpPltnVo>());
    private final String[] columnNames = {
            "#",
            "번호",
            "측정소명칭",
            "상태",
            "마지막 요청시각",
            "대기환경 실시간 현황"
    };

    public AtmpTableModel(List<AtmpPltnVo> ctlrList) {
        this.ctlrList = ctlrList;

        int indexCount = 1;
        for (AtmpPltnVo obj : ctlrList) {
            obj.setIndex(indexCount++);
        }
        if (ctlrList.size() == 0) {
            log.error("AtmpTableModel 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 AtmpPltnVo getControllerInfo(int row) {
        AtmpPltnVo info = this.ctlrList.get(row);
        return info;
    }

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

            switch (columnIndex) {
                case 0:
                    returnValue = info.getIndex();
                    break;
                case 1:
                    returnValue = info.getATMP_STTN_NMBR();
                    break;
                case 2:
                    returnValue = info.getATMP_STTN_NM();
                    break;
                case 3:
                    returnValue = info.getRequestResult() == 0 ? "실패" : "성공";
                    break;
                case 4:
                    returnValue = info.getRequestTime();
                    break;
                case 5:
                    returnValue = info.getValue();
                    break;
            }
        }
        return returnValue;
    }

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

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

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

}
