
import Panel from "../views/Panel";
import PanelCtrl from "../ctrls/panelCtrl";
import { DataManager } from "../../libs/tw/manager/DataManager";
import SceneCtrl from "../ctrls/sceneCtrl";
import { ModuleTypes } from "../types/sceneTypes";
import { Buried } from "../../libs/tw/util/Buried";
import { NetManager } from "../../libs/tw/manager/NetManager";
import Loading from "../loading/Loading";

export default class AcceptPanel extends Panel {

    public closepopBtn:eui.Button;//取消按钮
    public acceptBtn:eui.Button;//确定按钮
   
    public failBtn:eui.Button;
    public contentLab:eui.Label;//内容

    public canAcceptGroup:eui.Group;
    public failAcceptGroup:eui.Group;

    public clickBg:eui.Group;
    private mData:any;
    public get skinKey() { return 'AcceptPanel' }
    public get groupName() { return 'accept_panel' }
    
    constructor(data?){
        super(data);
        this.mData = data;
    }

    onSkinComplete():void{
        super.onSkinComplete();
        this.once(egret.Event.ADDED_TO_STAGE,this.onLoad,this);
		if(this.stage){
			this.onLoad();
        }
        this.showAct();
    }

    start():void{
        this.percentHeight = 100;
        
    }
    private onLoad():void{
        this.init();
    }   
    private init(){
        this.canAcceptGroup.visible = false;
        this.failAcceptGroup.visible = false;
        if(this.mData){
            this.canAcceptGroup.visible = this.mData.acceptResult;
            this.failAcceptGroup.visible = !this.mData.acceptResult;
        }
        if(this.mData && this.mData.content){
            this.contentLab.text = this.mData.content;
        }
 
    }
    initEvents():void{
        super.initEvents();
        this.closepopBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_cancel, this);
        this.failBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_cancel, this);
        
        this.acceptBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ok, this);
        this.clickBg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_cancel, this);
    }

    removeEvents():void{
        super.removeEvents();
        this.closepopBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_cancel, this);
        this.failBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_cancel, this);
        this.acceptBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_ok, this);
        this.clickBg.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap_cancel, this);
    }


    onTap_ok(e:egret.TouchEvent){
        PanelCtrl.instance.btnDelay(this.acceptBtn);
        if(!this.mData.shareCode){
            PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '助力码有误，请稍后重试~' });
            return;
        }
        Loading.instace.show()
        NetManager.ins.accept((success, result) => {
            Loading.instace.hide();
            this.hidePanel();
            if (!result) {
                PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '网络异常，请重试~' });
                return;
            }
            if (!success) {
                switch (result.code) {
                    case '100001':
                        PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '请先登录~' });
                        break;
                    case '100101':
                        PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '活动已结束~' });
                        break;
                    case '100013':
                        //PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '入参错误~' });
                        break;
                    case '100014':
                        PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '不能给自己助力哦~' });
                        break;
                    case '100015':
                        PanelCtrl.instance.show(ModuleTypes.ACCEPT_PANEL, {acceptResult:false,content: "已经给该好友助力过啦，\n本次不计入每天助力次数，\n快去给别的好友助力吧～"});
                        break;
                    case '300301':
                        PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '邀请码不存在~' });
                        break;
                    case '300302':
                        PanelCtrl.instance.show(ModuleTypes.ACCEPT_PANEL, {acceptResult:false,content: "该好友已经被助力5次啦，\n本次不计入每天助力次数，\n快去给别的好友助力吧～"});
                        break;
                    case '300303':
                        PanelCtrl.instance.show(ModuleTypes.ACCEPT_PANEL, {acceptResult:false,content: "每天最多可助力5个好友哦，\n明天继续助力吧~"});
                        break;
                    default:
                        break;
                }
            } else {
                PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL, { msg: '助力成功~' });
            }
        }, this.mData.shareCode)
    }
    onTap_cancel(e:egret.TouchEvent){
        PanelCtrl.instance.btnDelay(this.closepopBtn);
        this.hidePanel();
    }
    
}