import { DoJoinPlugDrawData } from '../../libs/tw/data/plug/doJoinPlugDraw/DoJoinPlugDrawData';
import { Buried } from '../../libs/tw/util/Buried';
import { DataManager } from '../../libs/tw/manager/DataManager';
import { ModuleTypes } from '../types/sceneTypes';
import { NetManager } from "../../libs/tw/manager/NetManager";
import Panel from "../views/Panel";
import SceneCtrl from '../ctrls/sceneCtrl';
import PanelCtrl from '../ctrls/panelCtrl';
import Waiting from '../waiting/Waiting';
export default class WarningPanel extends Panel {


    set message(val) {

    }
    set btnTxt(val) {

    }

    private scene;
    start(data) {
        this.data.message = data ? (data.msg ? data.msg : '网络出现问题请稍后再试~') : '网络出现问题请稍后再试~';
        this.data.type = data ? (data.type ? data.type : '') : '';
        this.data.activityId = data ? (data.activityId ? data.activityId : '0') : '0';
        this.data.exposure = data ? (data.exposure ? data.exposure : null) : null;
        this.scene = data ? (data.scene ? data.scene : null) : null;
        switch (this.data.type) {
            case "bomb":
                this['warningTitle'].text = `炸弹功能`
                this['warningTxt'].text = `使用炸弹功能后，您任意选择一个元素\n该元素的横竖各一排消除\n使用需要15积分哦～`
                break;
            case "magic":
                this['warningTitle'].text = `魔法棒功能`
                this['warningTxt'].text = `使用魔法棒功能后，您任意选择一个元素\n该元素的所有的相同元素消除\n使用需要10积分哦～`
                break;
            case "addCount":
                this['warningTitle'].text = `加步数功能`
                this['warningTxt'].text = `使用加步数功能后，增加剩余步数5步\n使用需要消耗20积分哦～`
                break;
            default:
                break;
        }
    }


    protected get creditsOutBtns(): eui.Button[] {
        return [this['creditsOutBtn']]
    }

    protected get closeBtns(): eui.Button[] {
        return [this['closeBtn']]
    }

    public closeBtn: eui.Button;
    public sureBtn: eui.Button;
    public cancelBtn: eui.Button;
    initEvents() {
        this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_closeBtn, this);
        this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_closeBtn, this);
        this.sureBtn.once(egret.TouchEvent.TOUCH_TAP, this.onTap_sureBtn, this);
    }

    removeEvents() {
        this.closeBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_closeBtn, this);
        this.cancelBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_closeBtn, this);
    }

    onTap_closeBtn(e: egret.TouchEvent) {
        this.hidePanel();

    }

    onTap_sureBtn() {
        let scope = this;
        Waiting.instance.show()

        this.doJoinCon(this.data.activityId, () => {
            Waiting.instance.hide()
            if (scope.data.exposure) {
                NetManager.ins.clickLog(scope.data.exposure);
            }
            switch (scope.data.type) {
                case 'bomb':

                    scope.scene.isBombable = true;
                    scope.scene.useBomb();
                    break;
                case 'magic':
                    scope.scene.isMagicable = true;
                    scope.scene.useMagic();
                    break;
                case 'addCount':
                    scope.scene.isAddCountable = true;
                    scope.scene.useAddCount();
                    break;
                default:
                    break;
            }

            this.hidePanel();
        })
    }



    //使用道具扣积分
    doJoinCon(activityId: number, callback: Function) {
        NetManager.ins.doJoinPlugDraw((isSuccess: boolean, msgData) => {
            if (isSuccess) {
                NetManager.ins.getPlugOrderStatus((success: boolean, data) => {
                    if (success) {
                        // 成功消耗积分
                        let isConsume = (DataManager.ins.getPlugOrderStatusData && DataManager.ins.getPlugOrderStatusData.result !== -1);
                        if (isConsume) {
                            callback()
                        } else {
                            PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, { msg: "道具使用失败，请稍后再试~", title: "很遗憾", type: "tips" });
                            this.scene.isBtnEnable = true;
                            this.scene.btnEnable(this.scene.isBtnEnable)
                        }
                    } else {
                        //失败
                        if (DataManager.ins.getPlugOrderStatusData && DataManager.ins.getPlugOrderStatusData.result == 0) {
                            //轮询失败
                            PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, { msg: "道具使用失败，请稍后再试~", title: "很遗憾", type: "tips" });
                            this.scene.isBtnEnable = true;
                            this.scene.btnEnable(this.scene.isBtnEnable)
                        } else {
                            // 结果失败
                            PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, { msg: "道具使用失败，请稍后再试~", title: "很遗憾", type: "tips" });
                            this.scene.isBtnEnable = true;
                            this.scene.btnEnable(this.scene.isBtnEnable)
                        }
                    }
                }, DataManager.ins.doJoinPlugDrawData.orderId, () => {
                    return (DataManager.ins.getPlugOrderStatusData.result == 0)
                })
            } else {
                // 下订单失败
                console.log(msgData);
                PanelCtrl.instance.show(ModuleTypes.MSG_PANEL, { msg: `${msgData && msgData.message ? msgData.message : "道具使用失败，请稍后再试~"}`, title: "很遗憾", type: "tips" });
                this.scene.isBtnEnable = true;
                this.scene.btnEnable(this.scene.isBtnEnable)
            }
        }, activityId, true)
    }

    protected get skinKey() { return 'Warning' }
}