package com.inspect.patrol.linkage.notify;

import com.inspect.patrol.domain.LinkageTaskInfo;
import com.inspect.patrol.domain.PopupData;
import com.inspect.patrol.domain.PopupInfoData;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class PopupResultMessage {
    private String deviceName;
    private List<PopupResultMessage.Item> items = new ArrayList<>();

    public PopupResultMessage(List<NotifyMessage> notifyMessages, PopupData popupData) {
        this.deviceName = popupData.getPatrolLinkagePos().getDeviceName();
        List<PopupInfoData> popupInfoDataList = popupData.getPopupInfoDataList();
        List<LinkageTaskInfo> linkageTaskInfos = new ArrayList<>();
        popupInfoDataList.forEach((item) -> {
            item.getList().forEach((i) -> {
                linkageTaskInfos.add(i);
            });
        });
        notifyMessages.forEach((item) -> {
            LinkageTaskInfo linkageTaskInfo = (LinkageTaskInfo) ((List) linkageTaskInfos.stream().filter((i) -> {
                return i.getPatrolPointId().equals(item.getObjectId());
            }).collect(Collectors.toList())).get(0);
            String collect = item.getResults().stream().map((i) -> {
                return i.getDesc();
            }).collect(Collectors.joining(","));
            this.items.add(new PopupResultMessage.Item(linkageTaskInfo.getPatrolPointName(), collect));
        });
    }

    public String getDeviceName() {
        return this.deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }

    public List<PopupResultMessage.Item> getItems() {
        return this.items;
    }

    public void setItems(List<PopupResultMessage.Item> items) {
        this.items = items;
    }

    public List<String> toResult() {
        List<String> result = new ArrayList<>();

        for (Item item : this.items) {
            result.add(String.format("%s %s %s", this.deviceName, item.getPatrolPointName(), item.getDesc()));
        }

        return result;
    }

    class Item {
        private String patrolPointName;
        private String desc;

        public Item(String patrolPointName, String desc) {
            this.patrolPointName = patrolPointName;
            this.desc = desc;
        }

        public String getPatrolPointName() {
            return this.patrolPointName;
        }

        public void setPatrolPointName(String patrolPointName) {
            this.patrolPointName = patrolPointName;
        }

        public String getDesc() {
            return this.desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
