package com.its.wthr.ui;

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

    private List<FrcsZoneVo> ctlrList = Collections.synchronizedList(new ArrayList<FrcsZoneVo>());
    private final String[] columnNames = {
            "#",
            "구역코드",
            "예보구역",
            "상태",
            "마지막 요청시각",
            "동네예보 실시간 현황"
    };

    public FrcsTableModel(List<FrcsZoneVo> ctlrList) {
        this.ctlrList = ctlrList;

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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object returnValue = null;
        synchronized (this.ctlrList) {
            FrcsZoneVo info = this.ctlrList.get(rowIndex);
            if (info == null) {
                return "";
            }
            switch (columnIndex) {
                case 0:
                    returnValue = info.getIndex();
                    break;
                case 1:
                    returnValue = info.getVILG_FRCS_ZONE_CD();
                    break;
                case 2:
                    returnValue = info.getVILG_FRCS_ZONE_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) {
            FrcsZoneVo 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(FrcsZoneVo info) {
        int index = 0;
        synchronized (this.ctlrList) {
            index = this.ctlrList.size();
            this.ctlrList.add(info);
        }
        fireTableRowsInserted(index, index);
    }

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

}
