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



export default class ReceivePanel extends Panel {
    private mdata: any;

    public receiveBtn:eui.Button;
    public accountEditable:eui.EditableText;
    public nameEditable:eui.EditableText;
    public redNumLab:eui.Label;
    public receiveBackBtn:eui.Button;

    public accountLab:eui.Label;
    public nameLab:eui.Label;

    private isAndroid:boolean = false;
    private isIos:boolean = false;

    public get skinKey() { return 'ReceivePanel' }
    public get groupName() { return 'receive_panel' }
 
    constructor(data: any) {
        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(function(){
            
        }.bind(this));
    }

    start(): void {
        this.percentHeight = 100;
    }

    initEvents(): void {
        super.initEvents(); 
        this.receiveBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickReceive,this);
        this.receiveBackBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickBack,this);

        this.accountEditable.addEventListener(egret.FocusEvent.CHANGE,this.setAccountLab,this);
        this.nameEditable.addEventListener(egret.FocusEvent.CHANGE,this.setNameLab,this);

        this.accountEditable.addEventListener(egret.FocusEvent.FOCUS_OUT,this.setFocusout,this);
        this.nameEditable.addEventListener(egret.FocusEvent.FOCUS_OUT,this.setFocusout,this);
    }

    removeEvents(): void {
        super.removeEvents();
        this.receiveBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickReceive,this);
        this.receiveBackBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClickBack,this);

        this.accountEditable.removeEventListener(egret.FocusEvent.CHANGE,this.setAccountLab,this);
        this.nameEditable.removeEventListener(egret.FocusEvent.CHANGE,this.setNameLab,this);

        this.accountEditable.removeEventListener(egret.FocusEvent.FOCUS_OUT,this.setFocusout,this);
        this.nameEditable.removeEventListener(egret.FocusEvent.FOCUS_OUT,this.setFocusout,this);

        
    }

    private onLoad() {
        this.init();
    }
    private init(){
        //str.replace(/(^\s*)|(\s*$)/g, "");
        if(this.mdata && this.mdata.redNum){
            this.redNumLab.text = this.mdata.redNum;
        }
        this.accountEditable.prompt = '请输入支付宝账号';
        this.accountEditable.restrict = "a-z A-Z 0-9 @ . _ -";

        this.nameEditable.prompt = '请输入姓名';
        this.nameEditable.restrict = "^0-9";

        let u = navigator.userAgent;
        this.isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
        this.isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if(this.isIos)
        {
            this.accountLab.textColor = 0xA4A3A3;
            this.nameLab.textColor = 0xA4A3A3;
            console.log('ios');
        }
    }

    private onClickReceive(e:egret.TouchEvent):void{
        PanelCtrl.instance.btnDelay(this.receiveBtn);

        let accountString = this.accountEditable.text;
        let nameString = this.nameEditable.text;
        
        if(this.judgeEmail(accountString) || this.judgePhone(accountString)){
            console.log('是支付宝账号');
        }else{
            PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL,{msg:'请输入正确的支付宝账号'});
            return;
        }
        // if(this.judgeName(nameString)){
        //     console.log('姓名格式正确')
        // }else{
        if(nameString == ''){
            PanelCtrl.instance.show(ModuleTypes.TOAST_PANEL,{msg:'姓名格式有误，请重新输入'});
            return;
        }
        PanelCtrl.instance.show(ModuleTypes.HITT_PANEL,{title:'提示',content:`你将为账号\n${accountString}(${nameString})充值支付宝现金${this.mdata.redNum}元`,callBackeOk:function(){console.log('确定充值');this.receivePrize(accountString,nameString);}.bind(this)});
    }
    //检查是否是正确的手机号
    private judgePhone(accountString:string){
        let phone = /^1(3|4|5|6|7|8|9)\d{9}$/;
        if(phone.test(accountString)){
            return true
        }else{
            return false;
        }
    }
    //检测是否是正确的邮箱
    private judgeEmail(accountString:string){
        let reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
        if(reg.test(accountString)){
            let tmpSplit =  accountString.split('@');
            let splitString = tmpSplit[1].split('.');
            splitString.shift();
            for (let index = 0; index < splitString.length; index++) {
                let elementString = splitString[index];
                if(elementString != 'com' && elementString != 'cn'){
                    return false;
                }
            }
            return true;
        }else{
            return false;
        }
    }
    //检测中文名字
    private judgeName(nameString){
        for (let index = 0; index < nameString.length; index++) {
            const element = nameString[index];
            if(!this.judgeChinese(element)){
                return false;
            }
        }
        return true;
    }
    //检测是否为汉字
    private judgeChinese(temp){
        if (temp.match(".*[\u4e00-\u9fa5].*")){
            return true;
        }
        return false;
    }
    private setAccountLab(){
        if(this.isIos){
            this.accountLab.text = this.accountEditable.text;
        }
    }
    private setNameLab(){
        if(this.isIos){
            this.nameLab.text = this.nameEditable.text;
        }
    }
    private  setFocusout(){
        if(this.accountEditable.text != ''){
            this.accountEditable.alpha = 0;
        }
        if(this.nameEditable.text != ''){
            this.nameEditable.alpha = 0;
        }
       
    }
    private onClickBack(){
        PanelCtrl.instance.btnDelay(this.receiveBackBtn);
        this.hidePanel();
    }
}