import { GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import { __FirstPayData, __FirstPayRewardData } from "../../data/sdata/SManage";
import ShopView from "../shop/ShopView";

const { ccclass, property } = cc._decorator;

interface FirstPayData {
    firstPay: {
        open: number,
        canGet: boolean,
        count: number,
    },
    data: Array<__FirstPayData>
}

enum Type {
    data = "data",
    get = "get",
}

@ccclass
export default class FirstPay extends ViewObject {

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

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

    firstPayData: FirstPayData = null;
    canGet: boolean = true;
    target: cc.Node = null;

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


    getFirstPayData(type: Type | string) {
        let msg = {};
        this.main.gameHttp.sendJson(`firstPay/v1/${type}`, msg, (state, reve: ReveData) => {
            this.main.stopLoad();
            if (state == HttpStateType.SUCCESS) {
                if (reve.retCode == 0) {
                    console.log("==reve=getFirstPayData===", reve)
                    if (type == Type.data) {
                        this.firstPayData = reve.data;
                        this.initItem();
                        this.show();
                    } else if (type == Type.get) {
                        this.refresh(this.target);
                        this.main.showReward(reve);
                    }
                } else {
                    this.main.showTips(reve.message);
                }
            } else {
                this.main.showTips('网络异常');
            }
        });
    }

    initItem() {
        this.canGet = this.firstPayData.firstPay.canGet;
        this.firstPayData.data.forEach((data, index) => {
            let parent = this.itemList[index].getChildByName("goodList");
            JSON.parse(data.reward).forEach(rd => {
                this.addGood(parent, rd);
            });
        })
        this.initBtn();
    }

    initBtn() {
        if (this.firstPayData.firstPay.open) {
            this.itemList.forEach((item, index) => {
                let btnPay = item.getChildByName("btn_pay");
                let btnGet = item.getChildByName("btn_get");
                let lbGet = btnGet.getChildByName("lbget");
                let lbGeted = btnGet.getChildByName("lbgeted");
                btnGet.active = true;
                btnPay.active = false;
                if (index > this.firstPayData.firstPay.count) {
                    btnGet.getComponent(cc.Button).interactable = false;
                    lbGet.active = true;
                    lbGeted.active = false;
                } else if (index < this.firstPayData.firstPay.count) {
                    btnGet.getComponent(cc.Button).interactable = false;
                    lbGet.active = false;
                    lbGeted.active = true;
                } else if (index == this.firstPayData.firstPay.count) {
                    lbGet.active = true;
                    lbGeted.active = false;
                }
                if (!this.firstPayData.firstPay.canGet) {
                    btnGet.getComponent(cc.Button).interactable = false;
                }
            })
        } else {
            this.itemList.forEach(item => {
                item.getChildByName("btn_pay").active = true;
                item.getChildByName("btn_get").active = false;
            })
        }
    }

    addGood(parent: cc.Node, reward: __FirstPayRewardData) {
        let good = cc.instantiate(this.goodItem);
        parent.addChild(good);
        this.initGoodItem(good, reward);
    }

    initGoodItem(item: cc.Node, reward: __FirstPayRewardData) {
        let node = item.getChildByName("infoNode");
        let icon = node.getChildByName("icon").getComponent(cc.Sprite);
        let frame = item.getChildByName("frame").getComponent(cc.Sprite);
        this.initIcon(icon, reward.icon);
        this.initFrameIcon(frame, reward.frame);
        node.getChildByName("countNode").getChildByName("count").getComponent(cc.Label).string = reward.count + "";
    }

    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;
            }
        });
    }

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

    onClickPay() {
        this.exitDistroy();
        this.main.viewManage.loadFunc(GameViewType.tap_shop, (viewObject: ViewObject) => {
            let shopView = viewObject as ShopView;
            shopView.init(Number(101));
            shopView.show();
        });
    }

    onClickGet(eventTouch: cc.Event.EventTouch, data: string) {
        if (this.canGet) {
            this.target = eventTouch.target;
            this.getFirstPayData(Type.get);
        }
    }

    refresh(node: cc.Node) {
        this.canGet = false;
        node.getComponent(cc.Button).interactable = false;
        node.getChildByName("lbgeted").active = true;
        node.getChildByName("lbget").active = false;
    }
}
