import { _decorator, Component, Label, Node, ProgressBar, Animation, sys } from 'cc';
import { userDataManager } from '../manager/userDataManager';
import { imageCacheManager } from '../manager/imageCacheManager';
import { GameManager } from '../GameManager';
import { SdkUtil } from '../sdkUtil';
import { area_item_data, userInfo } from '../data';
import { config } from '../config';
import { http } from '../http';
import { uiManager } from '../manager/uiManager';
import { select_area } from './select_area/select_area';
import { tools } from '../tools';
import { loading_view_auth } from './loading_view_auth';
import { dataManager } from '../manager/dataManager';
const { ccclass, property } = _decorator;

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

    startLoading(finish_load_call) {
        this.m_finish_load_call = finish_load_call
        SdkUtil.checkAuthUserInfo((is_true)=>{
            if(is_true) {
                this.loadingData()
                this.auth_view.active = false
            } else {
                this.auth_view.active = true
                this.auth_view.getComponent(loading_view_auth).initView(()=>{
                    this.auth_view.active = false
                    this.loadingData()
                })
            }
        })
    }

    private loadingData() {
        let progress = 0
        this.loading_bar.getComponent(ProgressBar).progress = progress;
        this.img_ride_bike.getComponent(Animation).play()
        this.schedule(()=>{
            if(this.m_pause_status){
                return;
            }
            progress += 0.1
            progress = parseFloat(progress.toFixed(1))
            // console.log('progress=',progress)
            
            this.loading_bar.getComponent(ProgressBar).progress = progress
            this.lab_loading_bar.getComponent(Label).string = `${(progress*100).toFixed()}%`

            switch (progress) {
                case 0.1:
                    this.m_pause_status = true
                    this.initUserData(()=>{
                        if(userDataManager.user_data.region_id<=0) {
                            this.setUserSelectArea()
                        } else {
                            this.m_pause_status = false
                        }
                        GameManager.openWebScoket()
                    })
                    break
                case 0.2:
                    this.m_pause_status = true
                    this.initSysConfig(()=>{
                        this.m_pause_status = false
                    })
                    break
                case 0.3:
                    this.m_pause_status = true
                    this.initMineRank(()=>{
                        this.m_pause_status = false
                    })
                    break
                case 0.4:
                    this.m_pause_status = true
                    this.initUserCarList(()=>{
                        this.m_pause_status = false
                    })
                    break
                case 0.5:
                    this.m_pause_status = true
                    this.initLevels(()=>{
                        this.m_pause_status = false
                    })
                    break
                case 0.6:
                    this.m_pause_status = true
                    this.initAllCarList(()=>{
                        this.m_pause_status = false
                    })
                    break
                case 0.7:
                    this.m_pause_status = true
                    this.initUserRedDotData(()=>{
                        this.m_pause_status = false
                    })
                    break
                case 0.8:
                    this.m_pause_status = true
                    imageCacheManager.start_cache(()=>{
                        this.m_pause_status = false
                    })
                    break
                default:
                    break;
            }

            if(progress >= 1) {
                if(this.m_finish_load_call!=null){
                    this.m_finish_load_call()
                }
                this.img_ride_bike.getComponent(Animation).stop()
                this.node.removeFromParent()
                this.unscheduleAllCallbacks()
                return;
            } 

        },0.1)
    }

    private initUserData(login_finish_cb){
        this.userLogin((openid)=>{
            // console.log('openid=',openid)
            SdkUtil.getUserInfo((data:userInfo)=>{
                let opt;
                let platform = dataManager.getPlatformId()
                if(data!=null){
                    opt = {"openid":openid,"platform":platform,"avatarUrl":data.avatarUrl,"nickName":data.nickName}
                }else{
                    opt = {"openid":openid,"platform":platform,"avatarUrl":"","nickName":"大靓仔"}
                }
                http.post(config.API.login,opt,(err,d)=>{
                    if(!err){
                        let nd = JSON.parse(d)
                        if(nd.code === config.status.SUCCESS){
                            userDataManager.user_data = nd.content
                            console.log('用户信息=',userDataManager.user_data)
                            GameManager.requestTTSidebarUserReward(config.USER_TT_SIDEBAR_REWARD.GET, (d_content)=>{
                                if(d_content==null) {
                                    if(login_finish_cb!=null){ login_finish_cb() }
                                    return
                                }
                                userDataManager.user_data.tt_sidebar_reward_status = 0
                                if(data!=null) {
                                    userDataManager.user_data.tt_sidebar_reward_status = d_content.status
                                } 
                                if(login_finish_cb!=null){
                                    login_finish_cb()
                                }
                            })
                        }
                    }
                })
            })
        })

    }

    private userLogin(cb){
        SdkUtil.login((res)=>{
            if(res!=null){
                let url = config.API.get_dyopen_id //默认抖音
                if(sys.platform == sys.Platform.WECHAT_GAME) {
                    if(SdkUtil.KS_GAME) { 
                        url = config.API.get_ksopen_id
                    } else { 
                        url = config.API.get_wxopen_id 
                    }
                }
                http.post(url,res,(err,data)=>{
                    if(!err){
                        let d = JSON.parse(data)
                        if(d.code === config.status.SUCCESS){
                            cb(d.content.openid)
                        }
                    }
                })
            }else{
                cb("123")
            }
        })
    }

    private setUserSelectArea() {
        uiManager.Instance().showUi(config.UI.ui_select_area, this.node, (node:Node)=>{
            node.getComponent(select_area).initView(false, (obj:select_area, data:area_item_data)=>{
                GameManager.requestUserSetRegion(data.id, 1, (d_content)=>{
                    userDataManager.user_data.region_id = d_content.region_id
                    userDataManager.user_data.region_pid = d_content.region_pid
                    userDataManager.user_data.province_name = d_content.province_name
                    userDataManager.user_data.city_name = d_content.city_name
                    userDataManager.user_data.license_code = d_content.license_code
                    this.m_pause_status = false
                    obj.closeSelf()
                },true)
            })
        })
    }

    private initSysConfig(cb) {
        http.get(config.STATIC_API.sysconfig,(err,d)=>{
            if(!err){
                let data = JSON.parse(d)
                if(data.code===config.status.SUCCESS){
                    tools.sys_config = data.content
                    // console.log('系统配置=',tools.sys_config)
                    if(cb!=null) {
                        cb()
                    }
                }
            }else{
                console.log("initSysConfig err",err)
            }
        })
    }

    private initMineRank(cb) {
        GameManager.requestMineRank(0, (d_content)=>{
            tools.mine_rank_data = d_content
            if(cb!=null) {
                cb()
            }
        })
    }

    private initUserCarList(cb){
        GameManager.requestUserCarList((d_content)=>{
            userDataManager.user_car_list = d_content
            // console.log('user_car_list=',userDataManager.user_car_list)
            if(cb!=null) {
                cb()
            }
        })
    }

    private initLevels(cb) {
        http.get(config.STATIC_API.levels,(err,d)=>{
            if(!err){
                let data = JSON.parse(d)
                if(data.code===config.status.SUCCESS){
                    tools.levels =  JSON.parse(data.content.levels_info)
                    tools.tpl_list = data.content.tpl_list
                    tools.game_config = JSON.parse(data.content.levels_config)
                    // console.log("tools.levels",tools.levels)
                    // console.log("tools.tpl_list",tools.tpl_list)
                    // console.log("tools.game_config",tools.game_config)
                    if(cb!=null) {
                        cb()
                    }
                }
            }else{
                console.log("initLevels err",err)
            }
        })
    }

    private initAllCarList(cb) {
        http.get(config.STATIC_API.car_list,(err,d)=>{
            if(!err){
                let data = JSON.parse(d)
                if(data.code===config.status.SUCCESS){
                    tools.all_car_list =  data.content
                    // console.log("tools.all_car_list",tools.all_car_list)
                    let slice_size:number = 6
                    for (let i = 0; i < tools.all_car_list.length; i += slice_size) {
                        let group = tools.all_car_list.slice(i, i + slice_size)
                        tools.all_car_page_list.push(group)
                    }
                    if(cb!=null) {
                        cb()
                    }
                }
            }else{
                console.log("init all_car_list err",err)
            }
        })
    }

    private initUserRedDotData(cb) {
        http.post(config.API.user_red_dot, null, (err,d)=>{
            if(!err){
                let data = JSON.parse(d)
                if(data.code===config.status.SUCCESS){
                    tools.user_red_dot_data = data.content
                    // console.log("tools.user_red_dot_data",tools.user_red_dot_data)
                    if(cb!=null) {
                        cb()
                    }
                }
            }else{
                console.log("init userRedDotData err",err)
            }
        },'GET')
    }

}

