package xn.xxp.main;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import http.vo.response.LabBulletinBoardVo;
import http.vo.response.LabWarnVo;
import xn.xxp.home.adapter.SensorEnum;
import xn.xxp.mqtt.entity.BulletinBoardEntity;
import xn.xxp.widget.ItemBoardView;

public class MainBoardAdapter extends BaseQuickAdapter<LabBulletinBoardVo, BaseViewHolder> {
    private Map<String, BoardValue> mValueMap = new HashMap<>();
    private Set<String> mWarningSet = new HashSet<>();

    public MainBoardAdapter(int layoutResId) {
        super(layoutResId);
    }


    private boolean goneSensor(String funNum) {
        return SensorEnum.FIRE.getFunNum().equals(funNum);
    }

    @Override
    public void setNewInstance(@Nullable List<LabBulletinBoardVo> list) {
        List<LabBulletinBoardVo> newlyList = null;
        if (list != null) {
            newlyList = new ArrayList<>();
            for (LabBulletinBoardVo item : list) {
                if (!goneSensor(item.funNum)) {
                    newlyList.add(item);
                }
            }
        }
        if (newlyList != null) {
            mValueMap.clear();
            for (LabBulletinBoardVo item : newlyList) {
                mValueMap.put(item.deviceNo, new BoardValue(item.deviceValue, item.unit, item.formatWaring()));
            }
        }
        super.setNewInstance(list);

    }

    public void updateSensorValue(BulletinBoardEntity.Data data) {
        List<LabBulletinBoardVo> newlyList = null;
        if (data != null && data.functionStatuses != null) {
            newlyList = new ArrayList<>();
            for (LabBulletinBoardVo item : data.functionStatuses) {
                if (!goneSensor(item.funNum)) {
                    newlyList.add(item);
                }
            }
        }

        boolean valueChanged = mValueMap.size() != (newlyList != null ? newlyList.size() : 0);
        if (newlyList != null) {
            for (LabBulletinBoardVo item : newlyList) {
                BoardValue oldValue = mValueMap.get(item.deviceNo);
                if (!valueChanged) {
                    valueChanged = !oldValue.value().equals(item.deviceValue) || !oldValue.unit().equals(item.unit);
                }
                mValueMap.put(item.deviceNo, new BoardValue(item.deviceValue, item.unit, item.formatWaring()));
            }
        }

        if (valueChanged) {
            notifyDataSetChanged();
        }
    }

    public void updateWaringValue(List<LabWarnVo> data) {
        List<LabWarnVo.TriggerVo> newlyList = null;
        if (data != null) {
            newlyList = new ArrayList<>();
            for (LabWarnVo item : data) {
                if (item.firstTrigger() != null && !goneSensor(item.firstTrigger().deviceNo)) {
                    newlyList.add(item.firstTrigger());
                }
            }
        }

        boolean valueChanged = mWarningSet.size() != (newlyList != null ? newlyList.size() : 0);
        if (!valueChanged) {
            LabWarnVo.TriggerVo changedItem = null;
            if (newlyList != null) {
                for (LabWarnVo.TriggerVo item : newlyList) {
                    if (!mWarningSet.contains(item.deviceNo)) {
                        changedItem = item;
                        break;
                    }
                }
            }
            valueChanged = changedItem != null;
        }

        mWarningSet.clear();
        if (newlyList != null) {
            for (LabWarnVo.TriggerVo item : newlyList) {
                mWarningSet.add(item.deviceNo);
            }
        }

        if (valueChanged) {
            notifyDataSetChanged();
        }
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, LabBulletinBoardVo labBulletinBoardVo) {
        ItemBoardView itemView = (ItemBoardView) baseViewHolder.itemView;
        BoardValue itemVal = mValueMap.get(labBulletinBoardVo.deviceNo);
        itemView.setSensorInfo(labBulletinBoardVo.attributeName, labBulletinBoardVo.icon);
        itemView.setSensorValue(
                itemVal != null ? itemVal.value() : "",
                itemVal != null ? itemVal.unit() : "",
                mWarningSet.contains(labBulletinBoardVo.deviceNo),
                labBulletinBoardVo.online
        );
    }

    private record BoardValue(String value, String unit, String formatVal) {
    }
}
