import { _decorator, assetManager, AudioClip, Component, instantiate, Label, log, Node, Prefab, resources, SpriteFrame, sys } from 'cc';
import { ads_config, server_han_zi_zhao_bu_tong_data, server_mei_nv_zhao_xi_jie_data, server_play_info, server_play_list_data, server_shuang_tu_zhao_bu_tong_data, server_sys_info, server_user_info, server_user_sign_status, unlock_levels, unlock_levels_item } from './data/server_play_list_data';
import { config } from './config';
import { ClientEvent } from './framework/clientEvent';
import { http } from './http/http';
import { StorageManager } from './framework/storageManager';
import { tools } from './tools';
import { AudioManager } from './framework/audioManager';
import { Util } from './framework/util';
import { wait_view } from './wait_view';
const { ccclass, property } = _decorator;

@ccclass('gameManager')
export class gameManager extends Component {
    public static g_server_sys_info:server_sys_info = new server_sys_info();
    public static g_ads_config:ads_config = new ads_config();
    public static g_server_play_list_data:server_play_list_data = new server_play_list_data();
    // public static g_server_shuang_tu_zhao_bu_tong_data:server_shuang_tu_zhao_bu_tong_data = new server_shuang_tu_zhao_bu_tong_data();
    // public static g_server_han_zi_zhao_bu_tong_data:server_han_zi_zhao_bu_tong_data = new server_han_zi_zhao_bu_tong_data();
    // public static g_server_mei_nv_zhao_xi_jie_data:server_mei_nv_zhao_xi_jie_data = new server_mei_nv_zhao_xi_jie_data();
    private static uiLayer:Node = null;
    private static topLayer:Node = null;
    private static waitLayer:Node = null;
    public static userInfo:server_user_info = new server_user_info();
    public static userSignStatus:server_user_sign_status = new server_user_sign_status();
    public static uiViewList:Map<string,Node> = new Map();
    public static topViewList:Map<string,Node> = new Map();
    public static user_level_data:unlock_levels = new unlock_levels();
    public static PreloadingLevelImgNumber:number = 0;//执行预加载的数量
    public static cache:Map<string,Map<string,SpriteFrame>> = new Map();
    public static mp3_cache:Map<string,AudioClip> = new Map();
    public static test_user_list:string[] = [];
    public static _instance: gameManager;
    public static isFinishLoad:boolean = false;
    public static havNoAllAd:boolean = false;

    public static get instance () {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new gameManager();
        return this._instance;
    }
    start() {
        
    }

    public static isTestUser(){
        for (let index = 0; index < gameManager.test_user_list.length; index++) {
            const element = gameManager.test_user_list[index];
            if(parseInt(element)==gameManager.userInfo.id){
                return true;
            }
        }
        return false;
    }

    public static init_test_user_list(list:string[]){
        gameManager.test_user_list = list;
    }
    public static init_unlock_levels(){
    
        for (let index = 0; index < gameManager.g_server_play_list_data.list.length; index++) {
            const element = gameManager.g_server_play_list_data.list[index];
            let item:unlock_levels_item = new unlock_levels_item;
            item.id = element.id;
            item.level = 1;
   
            if(gameManager.user_level_data.list[element.id]){

            }else{
                if(item.id===config.PLAY_TYPE.YI_QI_ZHAO_CHA||item.id===config.PLAY_TYPE.JI_YI_LI||item.id===config.PLAY_TYPE.DOU_DI_ZHU){
                    item.is_lock_game_play = 1;
                }else{
                    item.is_lock_game_play = 0;
                }
                gameManager.user_level_data.list[element.id] = item;
            }
       
        }
    }

    public static update_unlock_levels(){
        for (let index = 0; index < gameManager.g_server_play_list_data.list.length; index++) {
            const element = gameManager.g_server_play_list_data.list[index];
            if(element.id===config.PLAY_TYPE.YI_QI_ZHAO_CHA||element.id===config.PLAY_TYPE.JI_YI_LI||element.id===config.PLAY_TYPE.DOU_DI_ZHU){
                gameManager.user_level_data.list[element.id].is_lock_game_play = 1;
            }
       
        }
    }
    public static isFinishAllLevel(categoryid:number):boolean{
        let item = gameManager.get_cur_level_by_categoryid(categoryid)
        if(item.level===gameManager.get_play_level_number(categoryid)){
            return true;
        }
        return false;
    }
    public static get_play_level_number(categoryid:number):number{
        let list = gameManager.g_server_play_list_data.list;
        let number = 0;
        for (let index = 0; index < list.length; index++) {
            if(categoryid===list[index].id){
                number = list[index].barrier_number;
                break;
            }
        }
        return number;
    
    }

    public static get_unlock_levels():string{
        if(gameManager.user_level_data.list.length>0){
            return JSON.stringify(gameManager.user_level_data.list);
        }
        return "";
    
    }

    public static set_unlock_levels(unlock_levels:string){
        gameManager.user_level_data.list = [];
        let temp = JSON.parse(unlock_levels);
        for (let index = 0; index < temp.length; index++) {
            const element = temp[index];
            if(element!=null&&element!=undefined){
                let item = new unlock_levels_item()
                item.id = element.id;
                if(item.id===config.PLAY_TYPE.YI_QI_ZHAO_CHA||item.id===config.PLAY_TYPE.JI_YI_LI){
                    item.is_lock_game_play = 1;
                }else{
                    item.is_lock_game_play = element.is_lock_game_play;
                }
                item.level = element.level;
                item.look_video_number = element.look_video_number!=undefined?element.look_video_number:0;
                gameManager.user_level_data.list[item.id] = item;
            }
        }

    }

    public static set_unlock_levels_data(item:unlock_levels_item){
        gameManager.user_level_data.list[item.id] = item;
    }

    public static setWaitLayer(node:Node){
        gameManager.waitLayer = node;
    }

    public static sync_data(call_back,sync_data_type:number = config.sync_data_type.ALL){
        let form_Data = null;
        if(sys.platform===sys.Platform.BYTEDANCE_MINI_GAME){
            if(sync_data_type===config.sync_data_type.ALL){
                form_Data = {"coin": gameManager.userInfo.coin+"","unlock_levels": gameManager.get_unlock_levels()}
            }else if(sync_data_type===config.sync_data_type.COIN){
                form_Data = {"coin": gameManager.userInfo.coin+""}
            }else if(sync_data_type===config.sync_data_type.LEVELS){
                form_Data = {"unlock_levels": gameManager.get_unlock_levels()}
            }
            
        }else{
            form_Data = new FormData();
            if(sync_data_type===config.sync_data_type.ALL){
                form_Data.append("coin", gameManager.userInfo.coin+"");
                form_Data.append("unlock_levels", gameManager.get_unlock_levels());
            }else if(sync_data_type===config.sync_data_type.COIN){
                form_Data.append("coin", gameManager.userInfo.coin+"");
            }else if(sync_data_type===config.sync_data_type.LEVELS){
                form_Data.append("unlock_levels", gameManager.get_unlock_levels());
            }
        }

        http.post(config.api_url.sync_data,form_Data,(_err,c_data)=>{
            if(!_err){
                let c__data = JSON.parse(c_data);
                console.log("c__data",form_Data)
                if(c__data.code==config.MSG_CODE.SUCCESS){
                    // ClientEvent.dispatchEvent(config.EVENT_MSG.ON_CHANGE_COIN_NUMBER);
                    call_back(true)
                }
            }else{
                if(_err==-1){
                    call_back(false)
                }
              
            }
         
        })
    }

    public static get_user_coin():number{
        return gameManager.userInfo.coin;
    }

    public static add_coin(coin:number){
        gameManager.userInfo.coin+=coin;
        gameManager.sync_data((is_sync)=>{},config.sync_data_type.COIN)
    }

    public static sub_coin(coin:number){
        gameManager.userInfo.coin-=coin;
        if( gameManager.userInfo.coin<0){
            gameManager.userInfo.coin = 0;
        }
        gameManager.sync_data((is_sync)=>{},config.sync_data_type.COIN)
    }

    public static showLoading(){
        gameManager.hideLoading();
        gameManager.addTopView(config.PREFAB_PATH.view_loading,null);
    }

    public static hideLoading(){
        if(gameManager.topViewList.get(config.PREFAB_PATH.view_loading)){
            gameManager.topViewList.get(config.PREFAB_PATH.view_loading).removeFromParent();
            gameManager.topViewList.set(config.PREFAB_PATH.view_loading,null);
        }
    }

    public static get_cur_level_by_categoryid(categoryid:number):unlock_levels_item{
        if(gameManager.user_level_data.list.length<=0){
            return null;
        } 
        return gameManager.user_level_data.list[categoryid];
        // switch (categoryid) {
        //     case config.PLAY_TYPE.YI_QI_ZHAO_CHA:
        //         return gameManager.user_level_data.list[config.PLAY_TYPE.YI_QI_ZHAO_CHA];
        //     case config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG:
        //         return gameManager.user_level_data.list[config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG];
        //     case config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE:
        //         return gameManager.user_level_data.list[config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE];
            
        // }
    }

    public static next_level(categoryid:number,call_back):boolean{
        let item = gameManager.get_cur_level_by_categoryid(categoryid);
        let play_item = gameManager.get_play_list_item_by_categoryid(categoryid);
        if( (item.level+1) <= play_item.barrier_number ){
            item.level+=1;
            gameManager.sync_data((is_sync)=>{
                if(is_sync){
                    gameManager.set_unlock_levels_data(item);
                }else{
                    item.level-=1;
                }
                call_back(is_sync)
               
            },config.sync_data_type.LEVELS)
            return true;
        }
        return false;

    }

    public static setUiLayer(node:Node){
        gameManager.uiLayer = node;
    }
    public static getUiLayer(){
        return gameManager.uiLayer;
    }
    public static getTopLayer():Node{
        return gameManager.topLayer;
    }  
    public static setTopLayer(node:Node){
        gameManager.topLayer = node;
    }  

    public static addUiView(parent_path:string,call_back){
        if(gameManager.uiLayer==null){
            console.log("gameManager.uiLayer -- null!");
            return;
        }
        gameManager.showWaitView();
        resources.load(parent_path, Prefab, (err, prefab) => {
            if(err){
                console.log("addUiView -- err::",err);
                return;
            }

            let newNode:Node = instantiate(prefab);
            newNode.parent = gameManager.uiLayer;
            gameManager.uiViewList.set(parent_path,newNode);
            gameManager.hideWaitView();
            if(call_back!=null){
                call_back(newNode);
            }
        });
    }

    public static addTopView(parent_path:string,call_back){
        if(gameManager.topLayer==null){
            console.log("gameManager.topLayer -- null!");
            return;
        }
        gameManager.showWaitView();
        resources.load(parent_path, Prefab, (err, prefab) => {
            if(err){
                console.log("addTopView -- err::",err);
                return;
            }

            let newNode:Node = instantiate(prefab);
            gameManager.topViewList.set(parent_path,newNode);
            newNode.parent = gameManager.topLayer;
            gameManager.hideWaitView();
            if(call_back!=null){
                call_back(newNode);
            }
        });
    }

    public static showWaitView(opacity:number = 0,text:string=""){
        gameManager.waitLayer.active = true;
        gameManager.waitLayer.getComponent(wait_view).showView(opacity,text)
    }
    public static hideWaitView(){
        gameManager.waitLayer.active = false;
    }
    
    public static set_server_user_data(data){
        gameManager.userInfo = data; 
        if(gameManager.userInfo.unlock_levels!=""){
            gameManager.set_unlock_levels(gameManager.userInfo.unlock_levels);
        }
        console.log("set_server_user_data::",data);
    }

    public static get_user_is_sign():boolean {
        if(gameManager.userSignStatus.status == 1) {
            return true
        }
        return false
    }

    public static set_server_user_sign_status(data) {
        gameManager.userSignStatus = data.content
        console.log("set_server_user_sign_status::",data, 'signStatus_status::',gameManager.userSignStatus.status);
    }

    public static set_server_play_list_data(data:server_play_info){
        gameManager.g_server_play_list_data.list = data.wf_list;
        gameManager.g_server_sys_info = data.sys_info;
        gameManager.g_ads_config = data.ads_config;
        ClientEvent.dispatchEvent(config.EVENT_MSG.ON_INIT_GAME_LIST);
        console.log("set_server_play_list_data::",data);
    }

    public static get_play_list_item_by_categoryid(categoryid:number){
        for (let index = 0; index <  gameManager.g_server_play_list_data.list.length; index++) {
            const element =  gameManager.g_server_play_list_data.list[index];
            if(element.id===categoryid){
                return element;
            }
            
        }
        return null;
    }

    public static PreloadingLevelImg(finish_call_back){
        if(gameManager.g_server_play_list_data.list.length<=0){
            return
        }
        if(gameManager.PreloadingLevelImgNumber<=0){
            let count = 0;
            let call_back = ()=>{
                count++;
                if(count>=gameManager.PreloadingLevelImgNumber){
                    gameManager.PreloadingLevelImgNumber = 0;
                    finish_call_back()
                }
            }
            for (let index = 0; index < gameManager.g_server_play_list_data.list.length; index++) {
                const element = gameManager.g_server_play_list_data.list[index];
                if(element.id===config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG||element.id===config.PLAY_TYPE.YI_QI_ZHAO_CHA){
                    let item = gameManager.get_cur_level_by_categoryid(element.id);
                    if((item.level+1)<=element.barrier_number){
                        let key = `${element.id}_${item.level+1}`;
                        // let str = StorageManager.instance.getGlobalData(key);
                        if(!gameManager.cache.get(`${item.level+1}_${element.id}`)){
                            gameManager.PreloadingLevelImgNumber++;
                            gameManager.loadLevelImg(element.id,item.level+1,call_back)
                        }
                    }
                }
           
            }
        }
    
    }

    public static loadLevelImg(category_id,level,call_back){
        let key = `${category_id}_${level}`;
        let list:Map<string,SpriteFrame> = new Map()
        http.get(config.static_url.levels_info(category_id,level),(err,data)=>{
            if(!err){
                let _data = JSON.parse(data)
                if(_data.code===config.MSG_CODE.SUCCESS){
                    let data = _data.content

                    switch(category_id){
                        case config.PLAY_TYPE.YI_QI_ZHAO_CHA:
                           let img_count = 0;
                           let img_call_back = ()=>{
                                if(img_count>=2){
                                    console.log("key",key)
                                    StorageManager.instance.setGlobalData(key,"1")
                                    gameManager.cache.set(`${level}_${category_id}`,list)
                                    call_back();
                                }
                           }
                            tools.loadRemoteImg(data.img1,(sf:SpriteFrame)=>{
                                sf.addRef();
                                list.set(data.img1,sf);
                                img_count++;
                                img_call_back();
                            })
                            tools.loadRemoteImg(data.img2,(sf)=>{
                                sf.addRef();
                                list.set(data.img2,sf);
                                img_count++;
                                img_call_back();
                            })
                            break;
                        case config.PLAY_TYPE.HAN_ZI_ZHAO_BU_TONG:
                            let img_count_font = 0;
                            let img_call_back_font = ()=>{
                                if(img_count_font>=3){
                                    console.log("key",key)
                                    gameManager.cache.set(`${level}_${category_id}`,list)
                                    StorageManager.instance.setGlobalData(key,"1")
                                    call_back();
                                }
                            }
                            tools.loadRemoteImg(data.default_img,(sf)=>{
                                sf.addRef();
                                list.set(data.default_img,sf);
                                img_count_font++;
                                img_call_back_font();
                            })
                            tools.loadRemoteImg(data.find_img,(sf)=>{
                                sf.addRef();
                                list.set(data.find_img,sf);
                                img_count_font++;
                                img_call_back_font();
                            })
                            tools.loadRemoteImg(data.d_img,(sf)=>{
                                sf.addRef();
                                list.set(data.d_img,sf);
                                img_count_font++;
                                img_call_back_font();
                            })
                            break;     
                        case config.PLAY_TYPE.MEI_NV_ZHAO_XI_JIE:
                            let img_count_component = 0;
                            let img_call_back_component = ()=>{
                                if(img_count_component>=data.component.length+1){
                                    console.log("key",key)
                                    gameManager.cache.set(`${level}_${category_id}`,list)
                                    StorageManager.instance.setGlobalData(key,"1")
                                    call_back();
                                }
                            }
                            tools.loadRemoteImg(data.img,(sf)=>{
                                sf.addRef();
                                list.set(data.img,sf);
                                img_count_component++;
                                img_call_back_component();
                            })
                    
                            for (let index = 0; index < data.component.length; index++) {
                                const element = data.component[index];
                                tools.loadRemoteImg(element.img,(sf,id)=>{
                                    sf.addRef();
                                    list.set(element.img,sf);
                                    img_count_component++;
                                    img_call_back_component();
                                },element.id);
                            }
                            break;                                   
                    }
                }
            }

        })
    }

    //播放获取金币
    public static playAddCoinSound( loop:boolean = false){
        let path = gameManager.g_server_sys_info.sys_gain_coin_music;
        AudioManager.instance.playSound(path,loop)
    }

    //通用的关闭按钮点击音效
    public static playCloseBtnSound( loop:boolean = false){
        let path = gameManager.g_server_sys_info.sys_click_button_music;
        AudioManager.instance.playSound(path,loop)
    }
    //通用的按钮点击音效
    public static playBtnSound( loop:boolean = false){
        let path = gameManager.g_server_sys_info.sys_close_button_music;
        AudioManager.instance.playSound(path,loop)
    }
    //每个玩法的胜利音效
    public static playWinSoundByCategoryId(id:number, loop:boolean = false){
        let data = gameManager.get_play_list_item_by_categoryid(id)
        console.log("data.wf_fail_music",data.wf_fail_music)
        if(data.wf_victory_music!=null&&data.wf_victory_music!=""&&data.wf_victory_music!=undefined){
            let paht = data.wf_victory_music;
            AudioManager.instance.playSound(paht,loop)
        }
    
    }
    //每个玩法的失败音效
    public static playFailSoundByCategoryId(id:number, loop:boolean = false){
        let data = gameManager.get_play_list_item_by_categoryid(id);
        if(data.wf_fail_music!=null&&data.wf_fail_music!=""){
            let paht = data.wf_fail_music;
            AudioManager.instance.playSound(paht,loop)
        }
    }

    //每个玩法的正确音效
    public static playRightSoundByCategoryId(id:number, loop:boolean = false){
        let paht = gameManager.get_play_list_item_by_categoryid(id).wf_right_click_music;
        AudioManager.instance.playSound(paht,loop)
    }
    //每个玩法的错误音效
    public static playErrSoundByCategoryId(id:number, loop:boolean = false){
        let paht = gameManager.get_play_list_item_by_categoryid(id).wf_wrong_click_music;
        AudioManager.instance.playSound(paht,loop)
    }

    //每个玩法的背景音乐
    public static playGamePlayMusicByCategoryId(id:number, loop:boolean = false){
        let path = gameManager.get_play_list_item_by_categoryid(id).wf_piped_music;
        AudioManager.instance.playMusic(path,loop)
    }
    //主页的背景音乐
    public static playMainMusic(loop:boolean = false){
        let path = gameManager.g_server_sys_info.sys_piped_music;
        AudioManager.instance.playMusic(path,loop)
    }

    public static loadSysMusic(call_back){
        let count = 0;
        let call = ()=>{
            count++;
            if(count>=3){
                call_back()
            }
        }
        assetManager.loadRemote(gameManager.g_server_sys_info.sys_click_button_music,  (err: any, clip: AudioClip)=> {
            clip.addRef()
            call()
        });
        assetManager.loadRemote(gameManager.g_server_sys_info.sys_close_button_music,  (err: any, clip: AudioClip)=> {
            clip.addRef()
            call()
        });
        assetManager.loadRemote(gameManager.g_server_sys_info.sys_piped_music,  (err: any, clip: AudioClip)=> {
            clip.addRef()
            call()
        });
        assetManager.loadRemote(gameManager.g_server_sys_info.sys_gain_coin_music,  (err: any, clip: AudioClip)=> {
            clip.addRef()
            call()
        });
    }

    public static loadPlayMusicByCategoryId(id,call_back){
        let count = 0;
        let call = ()=>{
            count++;
            if(count>=5){
                call_back()
            }
        }
        let paly_item = gameManager.get_play_list_item_by_categoryid(id);
        if(!gameManager.mp3_cache.get(paly_item.wf_fail_music)){
            assetManager.loadRemote(paly_item.wf_fail_music,  (err: any, clip: any)=> {
                clip.addRef()
                gameManager.mp3_cache.set(paly_item.wf_fail_music,clip)
                call()
            });
        }else{
            call()
        }
        if(!gameManager.mp3_cache.get(paly_item.wf_victory_music)){
            assetManager.loadRemote(paly_item.wf_victory_music,  (err: any, clip: any)=> {
                clip.addRef()
                call()
            });
        }else{
            call()
        }

        if(!gameManager.mp3_cache.get(paly_item.wf_right_click_music)){
            assetManager.loadRemote(paly_item.wf_right_click_music,  (err: any, clip: any)=> {
                clip.addRef()
                gameManager.mp3_cache.set(paly_item.wf_right_click_music,clip)
                call()
            });
        }else{
            call()
        }

        if(!gameManager.mp3_cache.get(paly_item.wf_wrong_click_music)){
            assetManager.loadRemote(paly_item.wf_wrong_click_music,  (err: any, clip: any)=> {
                clip.addRef()
                gameManager.mp3_cache.set(paly_item.wf_wrong_click_music,clip)
                call()
            });
        }else{
            call()
        }

        if(!gameManager.mp3_cache.get(paly_item.wf_piped_music)){
            assetManager.loadRemote(paly_item.wf_piped_music,  (err: any, clip: any)=> {
                clip.addRef()
                gameManager.mp3_cache.set(paly_item.wf_piped_music,clip)
                call()
            });
        }else{
            call()
        }
   
    }

    public static decRefSf(categoryid:number){
        let levels_item = gameManager.get_cur_level_by_categoryid(categoryid);
        gameManager.cache.forEach((v,k)=>{
            if(v!=null){
                let l = k.split("_");
                let level = parseInt(l[0]);
                let _categoryid = parseInt(l[1]);
                if(_categoryid==categoryid){
                    if(level<levels_item.level){
                        v.forEach((v1,k1)=>{
                            v1.decRef()
                        })
                        gameManager.cache.set(`${level}_${_categoryid}`,null);
                    }
                }
            }
        })
    }

    public addCoinNumber(obj:Component,addnum:number,lab_number:Label,cur_num:number,call_back,Spacer=""){
        let _num = 0;
        let multiple = (addnum/100)<1?1:addnum/100;
        let schedule_num = 0.01/multiple; 
        obj.schedule( ()=>{
            _num+=(1*multiple);
            _num = Math.floor(_num);
            if(_num>=addnum){
                lab_number.string = Spacer+ (cur_num+_num);
                obj.unscheduleAllCallbacks()
                call_back();
                return
            }
            lab_number.string =  Spacer + (cur_num+_num);
        },schedule_num)
    }

    public static get_reward_number():number{
        let list =  gameManager.g_ads_config.reward.split(",");
        let start = parseInt(list[0]);
        let end = parseInt(list[1]);
        let reward = Util.getRandomInt(start,end);
        return reward;
    }

    public static get_deouble_reward_number():number{
        let list =  gameManager.g_ads_config.reward.split(",");
        let start = parseInt(list[0]);
        let end = parseInt(list[1]);
        let reward = Util.getRandomInt(start,end);

        list =  gameManager.g_ads_config.deouble_reward.split(",");
        start = parseInt(list[0]);
        end = parseInt(list[1]);
        reward *= Util.getRandomInt(start,end);
        return reward;
    }

    public static get_sign_reward_number():number {
        let list =  gameManager.g_ads_config.sign_reward.split(",");
        let start = parseInt(list[0]);
        let end = parseInt(list[1]);
        if(start == end) {
            return start
        }
        let reward = Util.getRandomInt(start,end);
        return reward;
    }

    // public static set_server_han_zi_zhao_bu_tong_data(data){
    //     gameManager.g_server_han_zi_zhao_bu_tong_data.list = data;
    //     console.log("set_server_han_zi_zhao_bu_tong_data::",data);
    // }

    // public static set_server_mei_nv_zhao_xi_jie_data(data){
    //     gameManager.g_server_mei_nv_zhao_xi_jie_data.list = data;
    //     console.log("set_server_mei_nv_zhao_xi_jie_data::",data);
    // }

    public static getUserId(){
        let user_id ="";
        try{
            if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
                user_id = tt.getStorageSync(config.USER_ID)
            }else{
                user_id = sys.localStorage.getItem(config.USER_ID)
            }
        }catch(e){
            console.log("sss")
        }
        return user_id;
    }

    public static setUserId(userid:string){
        if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
            tt.setStorageSync(config.USER_ID,userid)
        }else{
            sys.localStorage.setItem(config.USER_ID,userid)
        }
    }

    public static request_sign_status(type = config.SIGN_STATUS_TYPE.GET, successCallback:any, failCallback:any) {
        if(type == config.SIGN_STATUS_TYPE.SYNC) {
            gameManager.showWaitView()
        }
        http.post(config.api_url.sign_status,{"stype": type},(err,data) => {
            if(type == config.SIGN_STATUS_TYPE.SYNC) {
                gameManager.hideWaitView()
            }
            if(!err) {
                let _data = JSON.parse(data)
                if(_data.code==config.MSG_CODE.SUCCESS){
                    successCallback(_data)
                } else {
                    failCallback({'err': _data.code + _data.message + ''})
                }
            } else {
                failCallback(err)
            }
        })
    }
}


