import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import CUtilTime from "../../../util/CUtilTime";
import GoodItem from "../../common/GoodItem";
import TopMenu from "../TopMenu";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Revenge extends ViewObject {

    // id: 1001
    // name: "复仇礼包"
    // needCount: 2000
    // needType: 1
    // reveCount0: 1000
    // reveCount1: 1
    // reveCount2: 1
    // reveCount3: 1
    // reveId1: 1005
    // reveId2: 1105
    // reveId3: 1205
    // reveType0: 1
    // reveType1: 3
    // reveType2: 3
    // reveType3: 3

    // needType		    需要的货币类型，1：钻石；2：金币
    // reveType0		奖励的货币类型，1：钻石；2：金币
    // reveType1		奖励的道具类型，1：钻石；2：金币 ；3：装备；4：道具
    // reveType2		奖励的道具类型，1：钻石；2：金币 ；3：装备；4：道具
    // reveType3		奖励的道具类型，1：钻石；2：金币 ；3：装备；4：道具

    @property(cc.Label)
    title: cc.Label = null;

    @property(cc.Node)
    itemList: cc.Node[] = [];

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

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

    @property(cc.Label)
    lbnum: cc.Label = null;

    @property(cc.Label)
    revengeTime: cc.Label = null;


    revengeData = null;
    clickIndex: number = 1;
    curClickNode: cc.Node = null;

    buyClickFunc: Function = null;
    setBuyClickFunc(func: Function) {
        this.buyClickFunc = func;
    }

    /**
     * 
     * @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);
        }
    }

    getRevengeData() {
        let msg = {};
        this.main.gameHttp.sendJson(`revenge/v1/data`, msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    console.log("==reve=revengeData===", reve)
                    this.revengeData = reve.data.list[0];
                    this.initItem();
                    this.show();
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }

    initItem() {
        this.title.string = this.revengeData.name;
        this.goldIcon.active = this.revengeData.needType == 2;
        this.diamondIcon.active = this.revengeData.needType == 1;
        this.lbnum.string = this.revengeData.needCount;
        this.itemList.forEach((item, index) => {
            let node = item.getChildByName("item");
            let icon = node.getChildByName("icon").getComponent(cc.Sprite);
            let name = node.getChildByName("lbname").getComponent(cc.Label);
            let clickIcon = item.getChildByName("clickIcon");
            let count = node.getChildByName("count").getComponent(cc.Label);
            count.string = this.revengeData[`reveCount${index}`];
            if (index == 0) {
                let path = this.revengeData.reveType0 == 1 ? "3002" : "3001";
                this.initIcon(icon, "good/" + path);
            } else {
                if (this.revengeData[`reveType${index}`] == 3) {
                    this.initIcon(icon, "equip/" + this.revengeData[`reveId${index}`]);

                    let equip = this.main.sManage.getEquipById(this.revengeData[`reveId${index}`]);
                    name.string = equip.name;
                } else {
                    this.initIcon(icon, "good/" + this.revengeData[`reveId${index}`]);
                }
            }
            if (index == 1) {
                clickIcon.active = true;
                this.curClickNode = clickIcon;
            }
            if (index != 0) {
                item.getComponent(GoodItem).setCallback(() => {
                    this.curClickNode.active = false;
                    clickIcon.active = true;
                    this.curClickNode = clickIcon;
                    this.clickIndex = index;
                })
            }
        })
    }

    refreshTime(revengeTime: number) {
        if (this.revengeTime) {
            this.revengeTime.string = `${CUtilTime.getTimeString2(revengeTime)}后消失`;
        }
    }

    public initIcon(icon: cc.Sprite, path: string) {
        cc.resources.load(`icon/${path}`, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => {
            if (err) {
                cc.error(err);
            } else {
                icon.spriteFrame = spriteFrame;
            }
        });
    }

    onClickBuy() {
        let msg = {
            id: this.revengeData.id,
            index: this.clickIndex - 1,
        };
        this.main.gameHttp.sendJson(`revenge/v1/buy`, msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    this.buyClickFunc && this.buyClickFunc();
                    this.exitDistroy();
                    this.main.showReward(reve);
                    this.main.topNode.getComponent(TopMenu).refresh();
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }
}
