/**
 * 管理dapp相关信息
 */
import gameToast from "../Network/gameToast"
cc.Class({
    extends: cc.Component,

    properties: {
        avatarFarmerSprite: {
            default: null,
            tip: "默认的头像",
            type: cc.SpriteFrame,
            serializable: true,
        },
        avatarVillageSprite: {
            default: null,
            tip: "村长的头像",
            type: cc.SpriteFrame,
            serializable: true,
        },
        avatarMayorSprite: {
            default: null,
            tip: "镇长的头像",
            type: cc.SpriteFrame,
            serializable: true,
        },
        avatarNode: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        avatarButtonLabel: {
            default: null,
            type: cc.Label,
            serializable: true,
        },
        avatarTipLabel: {
            default: null,
            type: cc.Label,
            serializable: true,
        },

        avatarButtonBG: {
            default: null,
            type: cc.Sprite,
            serializable: true,
        },
        avatarTipBG: {
            default: null,
            type: cc.Sprite,
            serializable: true,
        },
        farmeTextBg: {
            default: null,
            tip: "村民文字图片",
            type: cc.SpriteFrame,
            serializable: true,
        },
        villageTitle: {
            default: '任职村长',
            serializable: true,
        },
        villageContent: {
            default: '村长可获得村民50%的CNT消费.',
            serializable: true,
        },
        villageText: {
            default: null,
            tip: "村长文字图片",
            type: cc.SpriteFrame,
            serializable: true,
        },
        villageButton: {
            default: null,
            tip: "村长按钮图片",
            type: cc.SpriteFrame,
            serializable: true,
        },

        mayorTitle: {
            default: '任职镇长',
            serializable: true,
        },
        mayorContent: {
            default: '镇长获得村民10%的CNT，成\n为镇长之后，村长的收益不会消\n失.',
            serializable: true,
        },

        mayorText: {
            default: null,
            tip: "镇长文字图片",
            type: cc.SpriteFrame,
            serializable: true,
        },
        mayorButton: {
            default: null,
            tip: "镇长按钮图片",
            type: cc.SpriteFrame,
            serializable: true,
        },
        villageAndMayorNode: {
            default: null,
            type: cc.Node,
            serializable: true
        },

        ToastParent: {
            default: null,
            type: cc.Node,
            serializable: true
        },

        //两个按钮，cnt 和 snb
        cntShowNode: {
            default: null,
            type: cc.Node,
            serializable: true
        },
        snbShowNode: {
            default: null,
            type: cc.Node,
            serializable: true
        },
        ListInfoNode: {
            default: null,
            type: cc.Node,
            serializable: true
        },
        snbPage: 1,
        snbLimit: 100,
        snbData: {
            default: null,
            visible: false
        },
        cntPage: 1,
        cntLimit: 100,
        cntData: {
            default: null,
            visible: false
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.villageAndMayorScript = this.villageAndMayorNode.getComponent("villageAndMayor");
        //开启两个列表面板
        //先加载数据
        GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
            const { err, res } = data;
            // if (err === null) {
            //     console.log(res) //
            // }
            this.cntData = res.data;
        });
        this.cntShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
            // let agent_level = GlobalD.Dapp.UserInfo.agent_level;
            // if(0 === agent_level){
            //     GlobalD.GameData.showToast(cc.find("Canvas/UICamera"), "请先升级村长！", 120);
            //     return;
            // }
            this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, true);
        }, this);
        //snb数据
        GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
            // console.log("==========",data);
            this.snbData = data.content;
        });

        this.snbShowNode.on(cc.Node.EventType.TOUCH_END, function (event) {
            this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, true);
        }, this);


    },

    onUpdateCntList() {
        GlobalD.GameData.onCntLog(this.cntPage, this.cntLimit, (data) => {
            const { err, res } = data;
            this.cntData = res.data;
            this.ListInfoNode.getComponent("dappListInfo").onShowList("cntList", this.cntData, false);
        });

    },
    onUpdateSnbList() {
        GlobalD.GameData.onGetSnbInfoList(this.snbPage, this.snbLimit, (data) => {
            // console.log("==========",data);
            this.snbData = data.content;
            this.ListInfoNode.getComponent("dappListInfo").onShowList("snbList", this.snbData, false);
        });
    },

    start() {
        /**
         * 用户信息初始化到头像部分
         * 
         */

        //    this.UserInfo =  {
        //         "id": 1, // 账户ID
        //         "pid": 0, // 上级ID，0未绑定，显示可绑定邀请码的按钮
        //         "address": "TDw6xsVnDJWsdRBLkWAwXbv4hE2X2JQs5z", // 账户地址
        //         "invite_code": "MzhISO1Mzt", // 本人邀请码
        //         "agent_level": 0, // 0普通用户，1 村长身份，2 镇长身份
        //         "create_time": "2021-12-30 09:23:18",
        //         "parent": null // 上级不存在，上级存在时，返回上级对象，属性同本消息体一致
        //     }
        this.InitAvatarInfo();
    },

    InitAvatarInfo() {
        let agent_level = GlobalD.Dapp.UserInfo.agent_level;  // 0普通用户，1 村长身份，2 镇长身份
        switch (agent_level) {
            case 0:
                this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
                // this.avatarButtonLabel.string = '申请村长';
                // this.avatarTipLabel.string = '村民';
                this.avatarButtonBG.spriteFrame = this.villageButton;
                this.avatarTipBG.spriteFrame = this.farmeTextBg;
                break;
            case 1:
                this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
                // this.avatarButtonLabel.string = '任职镇长';
                // this.avatarTipLabel.string = '村长';
                this.avatarButtonBG.spriteFrame = this.mayorButton;
                this.avatarTipBG.spriteFrame = this.villageText;
                break;
            case 2:
                this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
                // this.avatarButtonLabel.node.active = false;
                this.avatarButtonBG.node.parent.active = false;
                // this.avatarTipLabel.string = '镇长';
                this.avatarTipBG.spriteFrame = this.mayorText;
                break;

        }
    },

    /**
     * 根据按钮显示状态
     */
    onPromotionInfo() {
        let agent_level = GlobalD.Dapp.UserInfo.agent_level;  // 0普通用户，1 村长身份，2 镇长身份
        switch (agent_level) {
            case 0:
                this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarFarmerSprite;
                this.villageAndMayorScript.setVillageAndMayorInfo({
                    title: this.villageTitle, content: this.villageContent, confirmText: '申请', callback: () => {
                        this._becomeVillageChief();
                        this.villageAndMayorNode.active = false;
                    },
                    type: "village"
                });
                break;
            case 1:
                this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarVillageSprite;
                this.villageAndMayorScript.setVillageAndMayorInfo({
                    title: this.mayorTitle, content: this.mayorContent, confirmText: '任职', callback: () => {
                        this._applyMayor();
                        this.villageAndMayorNode.active = false;
                    },
                    type: "mayor"
                });
                break;
            case 2:
                this.avatarNode.getComponent(cc.Sprite).spriteFrame = this.avatarMayorSprite;
                console.log("你已经不需要升级了！");
                break;

        }
    },

    _becomeVillageChief() {
        if (!GlobalD.dapp) {
            console.error("onBecomeVillageChief:dapp未初始化！");
            return;
        }


        GlobalD.dapp.becomeVillageChief().then((data) => {
            console.log("申请村长:" + JSON.stringify(data));
            const [err, tx] = data;
            if (err === null) {
                console.log('申请成功：' + tx) // 申请提交成功，等待审核
                //成功设置村长状态，并且更新
                GlobalD.Dapp.UserInfo.agent_level = 1;
                this.InitAvatarInfo();
                GlobalD.GameData.showToast(this.ToastParent, "申请成功！", 3, () => {
                    console.log("finish toast!");
                });
            } else {
                console.log(err) // 申请失败
                GlobalD.GameData.showToast(this.ToastParent, err, 3, () => {
                    console.log("finish toast!");
                });
            }
        })

        // async function becomeInit() {
        //     try {

        //         const { err, tx } = await GlobalD.dapp.becomeVillageChief()
        //         if (err === null) {
        //             // TODO 成功, 任职村长需要区块确认，需要回调接口
        //             console.log(tx) // 交易hash，唯一标识符
        //         } else {
        //             console.log(err)
        //         }
        //     } catch (err) {
        //         console.error("申请村长:", JSON.stringify(err));
        //     }
        // }
        // becomeInit();


    },

    _applyMayor() {
        if (!GlobalD.dapp) {

            console.error("onApplyMayor:dapp未初始化！");
            return;
        }
        let _self = this;
        GlobalD.dapp.applyMayor().then((data) => {
            console.log("任职镇长:", JSON.stringify(data));
            if (data.err === null) {
                console.log(data.res) // 申请提交成功，等待审核
                GlobalD.GameData.showToast(_self.ToastParent, data.res, 3, () => {
                    console.log("finish toast!");
                });
            } else {
                console.log(data.err) // 申请失败
                GlobalD.GameData.showToast(_self.ToastParent, data.err, 3, () => {
                    console.log("finish toast!");
                });
            }
        })

    }



    // update (dt) {},
});
