import ViewObject from "../../main/ViewObject";
import { HttpStateType, ReveData } from "../../util/CHttp";
import RedPoint from "../data/RedPoint";
import SManage, { RewardData } from "../data/sdata/SManage";
import MailInfo from "./MailInfo";
import MailItem from "./MailItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Mail extends ViewObject {

    @property(cc.Node)
    mNull1: cc.Node = null;

    @property(cc.Node)
    mNull2: cc.Node = null;

    @property(cc.Node)
    mContent1: cc.Node = null;

    @property(cc.Node)
    mContent2: cc.Node = null;

    @property(cc.Prefab)
    mItem: cc.Prefab = null;

    @property(cc.Node)
    mInfoNode: cc.Node = null;//邮件详情节点

    @property(MailInfo)
    mMailInfo: MailInfo = null;

    @property(cc.Toggle)
    mToggle1: cc.Toggle = null;

    @property(cc.Toggle)
    mToggle2: cc.Toggle = null;

    itemList: cc.Node[] = [];

    clickIndex: number = 0;
    onLoad() {
        this.itemList = [];
        this.mNull1.active = false;
        this.mNull2.active = false;
        this.mInfoNode.active = false
        this.getMail();
    }
    /**
     * 
     * @param prev 父界面
     */
    public show(prev?: ViewObject) {
        if (prev) {
            this.prev = prev;
            this.prev.__close();
        }
        this.main.viewManage.popView1(this.node);
        if (this.main && this.main.gameHttp) {
            this.main.gameHttp.pushEvent(this);
        }
    }

    private getMail() {
        this.main.gameHttp.sendJson('email/v1/data', {}, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    this.initList(reve.data);
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }

    private initList(list: Array<any>) {
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            let node = cc.instantiate(this.mItem);
            let item = node.getComponent(MailItem);
            this.itemList.push(node);
            item.init(element, i)
            if (!element.check && !element.receive) {
                RedPoint.addRed(this.main, node)
            }
            item.setCallback((mailItem: MailItem) => {
                RedPoint.removeRed(mailItem.node)
                this.openInfo(mailItem);
                this.refreshStatus(mailItem);
            });
            if (element.type == 1) {
                node.parent = this.mContent2;
            } else {
                node.parent = this.mContent1;
            }

        }
        if (this.mContent1.children.length > 0) {
            this.mNull1.active = false
        } else {
            this.mNull1.active = true
        }

        if (this.mContent2.children.length > 0) {
            this.mNull2.active = false
        } else {
            this.mNull2.active = true
        }
    }

    private openInfo(mailItem: MailItem) {
        if (this.mMailInfo.mailItem == mailItem) {
            return
        }
        this.mMailInfo.setMail(mailItem)
        this.mMailInfo.getMailInfo();
    }
    public onclickOnekey() {
        let list: Array<RewardData> = []
        this.onkeyGetMail(0, list, () => {
            this.main.showRewardList(list)
        })
    }
    /**
     * 一键领取所有邮件
     */
    public onkeyGetMail(index: number, list: Array<RewardData>, callback: () => void) {
        let content: cc.Node = null
        if (this.mToggle1.isChecked) {
            content = this.mContent1
        } else {
            content = this.mContent2
        }

        let nodes = content.children
        if (index >= nodes.length) {
            callback()
            return
        }
        let mailItem = nodes[index].getComponent(MailItem)
        if (mailItem.data.receive) {//已经领取了
            index++
            this.onkeyGetMail(index, list, callback)
        } else {
            this.getMailInfo(mailItem, (temps) => {
                if (temps) {
                    for (let i = 0; i < temps.length; i++) {
                        const element = temps[i];
                        Mail.addReward(list, element)
                    }
                }
                index++
                this.onkeyGetMail(index, list, callback)
            })
        }
    }
    private static addReward(list: Array<RewardData>, data: RewardData) {
        if (data.type == 1 || data.type == 2) {
            for (let i = 0; i < list.length; i++) {
                const element = list[i];
                if (element.type == data.type) {
                    element.count += data.count
                    return
                }
            }
            list.push(data)
        } else if (data.type == 3) {//道具
            list.push(data)
        } else {
            list.push(data)
        }

    }


    public getMailInfo(mailItem: MailItem, callback: (list) => void) {
        let msg = {
            mailId: mailItem.data.id
        }
        this.main.gameHttp.sendJson('email/v1/receive', msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    mailItem.data.receive = true;
                    mailItem.tipsIcon.active = false;
                    RedPoint.removeRed(mailItem.node)
                    if (this.mMailInfo.mailItem == mailItem) {
                        this.mMailInfo.flush();
                    }
                    callback(this.main.sManage.getRewards(reve))
                } else {
                    this.main.showTips(reve.message);
                    callback(null)
                }
            } else {
                this.main.showTips('网络异常');
                callback(null)
            }
        });
    }

    public refreshStatus(mailItem: MailItem) {
        if (this.clickIndex == mailItem.index) {
            mailItem.node.color = cc.color(173, 173, 173, 255);
        } else {
            this.itemList.forEach(item => {
                if (item.getComponent(MailItem).index == this.clickIndex) {
                    item.color = cc.color(255, 255, 255, 255);
                }
            })
            mailItem.node.color = cc.color(173, 173, 173, 255);
            this.clickIndex = mailItem.index;
        }
    }

}