import ViewObject from "../main/ViewObject";
import { HttpStateType, ReveData } from "../util/CHttp";
import { GameViewType } from "../main/ViewManage";
/**
 * 凤起登陆
 */
const {ccclass, property} = cc._decorator;

interface FqLoginData{
    uid:string
    token:string
}



@ccclass
export default class FqLogin extends ViewObject {

    start () {
        if(cc.sys.isNative){
            if (cc.sys.os === cc.sys.OS_ANDROID) {
                let className = "com/funqueue/android/xsdmx/MainActivity";
                let methodName = "login";
                let methodSignature = "(Ljava/lang/String;)V";
                jsb.reflection.callStaticMethod(className, methodName, methodSignature,'"this is a message from js"');
                let myself = this
                window._fqAndroidLoginCallback = (str)=>{
                    console.log('window._fqAndroidLoginCallback' , str)
                    myself.loginAndroid(JSON.parse(str));
                    return '1'
                }
            } else if (cc.sys.os === cc.sys.OS_IOS) {
                jsb.reflection.callStaticMethod('AppController', 'showLogin');

                let myself = this
                window._fqIosLoginCallback = (str)=>{
                    console.log('window._fqIosLoginCallback' , str)
                    myself.loginIos(JSON.parse(str));
                    return '1'
                }
            }
            /**
             * 
             * @param str 切换账号回调
             * @returns 
             */
            window._switchAccountCallback = ()=>{
                console.log('window._fqIosLoginCallback')
                cc.director.loadScene('hotUpdate')
                return '1'
            }
        }
    }

    public loginAndroid(data:FqLoginData){
        let http = this.main.loginHttp;
        let msg = {
            'uid':data.uid,
            'token':data.token
        }
        this.main.startLoad();
        http.sendForm('/fqLogin_android',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.main.userData = reve.data;
                    this.openArea();
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        })
    }
    public loginIos(data:FqLoginData){
        let http = this.main.loginHttp;
        let msg = {
            'uid':data.uid,
            'token':data.token
        }
        this.main.startLoad();
        http.sendForm('/fqLogin_ios',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.main.userData = reve.data;
                    this.openArea();
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        })
    }

     /**
     * 打开选区界面
     */
    public openArea(){
        let main = this.main;
        main.startLoad()
        main.viewManage.loadFunc(GameViewType.area,(viewObject:ViewObject)=>{
            main.stopLoad()
            viewObject.show();
        });
        this.node.destroy()
    }
    /**
     * 提交数据到友盟
     * @param id 
     * @param key 
     * @param value 
     */
    public static commitEvent(id:string,key:string,value:string){
        let msg = {
            id:id,
            key:key,
            value:value
        }
        let jsonstr = JSON.stringify(msg);
        console.log("====打点统计=====", jsonstr);
        if (cc.sys.os === cc.sys.OS_ANDROID) {
            let className = "com/funqueue/android/xsdmx/MainActivity";
            let methodSignature = "(Ljava/lang/String;)V";
            jsb.reflection.callStaticMethod(className, 'commitEvent', methodSignature,JSON.stringify(msg));
        } else if (cc.sys.os === cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'commitEvent:',JSON.stringify(msg));
        }
    }

}
