import { _decorator, Component, Node, ProgressBar, sys } from 'cc';
import { sysMessage, UserData } from '../../../data/data';
import { gameManager } from '../gameManager';
import { http } from '../../http';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('loading_view')
export class loading_view extends Component {
    @property(Node) loading_bar:Node = null;
    private m_finish_load_call = null;
    private m_pause_status = false;


    public startLoading(finish_load_call){
        this.m_finish_load_call = finish_load_call;
        this.loading_bar.getComponent(ProgressBar).progress = 0;
        this.schedule(()=>{
            if(this.m_pause_status){
                return;
            }
            this.loading_bar.getComponent(ProgressBar).progress +=0.1;
            let progress = this.loading_bar.getComponent(ProgressBar).progress
            switch(progress){
                case 0.2:
                    this.m_pause_status = true;
                    http.run_get(http.get_sys_config(),(err,data)=>{
                        if(!err){
                            let sys_data:sysMessage = JSON.parse(data)
                            gameManager.setSysData(sys_data)
                            this.m_pause_status = false;
                        }
                    })
                break;
                case 0.5:
                    this.m_pause_status = true;
                    let user_data = gameManager.getUserData()
                    if(user_data==null){
                        user_data = new UserData;
                        user_data.user_id = "123";
                    }
                    let formData = {"openid":user_data.user_id,"avatarUrl": ""}
                    let login_call_back = ()=>{
                        // console.log("formData",formData)
                        http.run_post(http.get_login(),formData,(err,data)=>{
                            if(!err){
                                let _data = JSON.parse(data);
                                if(_data.code==config.MSG_CODE.SUCCESS){
                                    user_data.user_id =  _data.content.openid
                                    user_data.token = _data.content.token;
                                    if(_data.content.unlock_levels.length<=0){
                                        user_data.unlock_levels = []
                                    }
                                    gameManager.setUserData(user_data)
                                    if(user_data.unlock_levels.length<=0){
                                        gameManager.Singleton.unLockLevel(1)
                                    }
                                    config.TOKEN = user_data.token;
                                    this.m_pause_status = false;
                                }
                                return
                            }
                      
                        })
                    }
                    if(user_data.user_id==null){
                        if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME){
                            tt.login({
                                force: true,
                                success(res) {
                                    let code = res.code;
                                    let anonymousCode = res.anonymousCode;
                                    http.run_post(http.get_dyopen_id(),{"code":code,"anonymous_code":anonymousCode},()=>{
                                        login_call_back();
                                    })
                                }
                                ,fail(res) {
                                    console.log(`login 调用失败`);
                                }
                            });
                        }else{
                            login_call_back();
                        }
            
                    }else{
                        login_call_back();
                    }
                break;
            }
            if(this.loading_bar.getComponent(ProgressBar).progress>=1){
                if(this.m_finish_load_call!=null){
                    this.m_finish_load_call()
                }
                this.unscheduleAllCallbacks()
                return;
            } 
        },0.1)
    }
}


