// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import HTTPS, { NetGet, NetPost } from "./HTTPS";
import Tips from "./Tips";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Task extends cc.Component {

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


    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.Content.active = false

        let Close = this.node.getChildByName("Close")

        vv.deleteBtnEvent(Close, this)
        vv.addBtnEvent(Close, () => {
            this.node.active = false
        }, this)
    }



    protected onEnable(): void {
        this.getTask()
    }


    getTask() {
        HTTPS.Instance.get(NetGet.Task).then(res => {
            if (res.code != 200) {
                Tips.Instance.show(res.msg)
                return
            }

            let item: cc.Node = null

            for (const key in res.data) {
                if (Object.prototype.hasOwnProperty.call(res.data, key)) {
                    const element = res.data[key];

                    switch (key) {
                        case 'ad_watch':
                            item = this.Content.getChildByName(key)
                            item.getChildByName("title").getComponent(cc.Label).string = element.title
                            item.getChildByName("reward").getComponent(cc.Label).string = `${element.reward_min}-${element.reward_max}元`

                            //对接广告
                            vv.deleteBtnEvent(item, this)
                            vv.addBtnEvent(item, () => {
                                // this.setTask(key)
                                Tips.Instance.show('等待对接广告')
                            }, this)
                            break;
                        case 'sign_in':
                            item = this.Content.getChildByName(key)
                            item.getChildByName("title").getComponent(cc.Label).string = element.title
                            item.getChildByName("reward").getComponent(cc.Label).string = `${element.reward}元`
                            //每日签到人物
                            vv.deleteBtnEvent(item, this)
                            vv.addBtnEvent(item, () => {
                                this.setTask(key)
                            }, this)

                            break;
                        default:
                            break;
                    }
                }
            }


            this.Content.active = true
        })
    }

    submitTask() {
        Tips.Instance.show('等待对接广告')
    }

    setTask(_task_type) {
        HTTPS.Instance.post(NetPost.complete, {
            task_type: _task_type,
        }).then(res => {
            if (res.code != 200) {
                Tips.Instance.show(res.msg)
                return
            }

            Tips.Instance.show(res.data.message)
        })
    }
    // update (dt) {}
}
