import { _decorator, Component, Node, SpriteFrame, sys } from 'cc';
import { config } from './config';
import { settingData } from './data';
import { http } from './http';
import { imageCacheManager } from './manager/imageCacheManager';
import { userDataManager } from './manager/userDataManager';
import { SdkUtil } from './sdkUtil';
import { uiManager } from './manager/uiManager';
import { restart_view } from './ui/restart_view';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    public static openId:string = 'asd'
    public static settingData:settingData = null

    public static getUserDefaultHomeCarSf():SpriteFrame {
        let sf = imageCacheManager.getHomeCarImageById(userDataManager.user_car_list.default_car_id)
        return sf
    }

    public static getUserDefaultRankCarSf():SpriteFrame {
        let sf = imageCacheManager.getRankCarImageById(userDataManager.user_car_list.default_car_id)
        return sf
    }
    
    public static checkPlayGame(parent_node:Node,play_cb) {
        let call_back = (()=>{
            play_cb()
        })
        if(userDataManager.getUserIsFreeAds()) {
            call_back()
            return
        }
        if(userDataManager.isTodayCanFreePlayGame()) {
            userDataManager.addTodayPlayGameNumber()
            call_back()
            return
        }

        uiManager.Instance().showUi(config.UI.ui_restart_view, parent_node, (node:Node)=>{
            node.getComponent(restart_view).initView((v:restart_view)=>{
                GameManager.showVideoAd(config.ADS_TYPE.GAME_RESTART, ()=>{
                    v.closeSelf()
                    call_back()
                })
            },(v:restart_view)=>{
                // if(userDataManager.isTodayCanShare()) {
                //     userDataManager.addTodayShareNumber()
                //     v.closeSelf()
                //     call_back()
                //     return
                // }
                if(userDataManager.isTodayCanShare()==false) {
                    v.closeSelf()
                    call_back()
                    return
                }
                SdkUtil.shareGame('',(r)=>{
                    if(r==true) {
                        userDataManager.addTodayShareNumber()
                        v.closeSelf()
                        call_back()
                    }
                })
            })
        })
    }

    // 设置
    public static getSettingData():settingData {
        if(GameManager.settingData!=null) {
            return GameManager.settingData
        }
        let str = sys.localStorage.getItem(config.SETTING_DATA)
        if(str==undefined||str==""||str==null){
            GameManager.settingData = new settingData
        } else {
            let data:settingData = JSON.parse(str)
            GameManager.settingData = data
        }
        return GameManager.settingData;
    }

    public static saveSettingData(data:settingData) {
        sys.localStorage.setItem(config.SETTING_DATA, JSON.stringify(data));
    }

    public static vibrateShort() {
        if(GameManager.settingData.isOpenZhendong) {
            SdkUtil.vibrateShort()
        }
    }

    //请求广播
    public static requestGuangbo(cb=null) {
        http.get(config.STATIC_API.msg, (err,d)=>{
            if(!err){
                let nd = JSON.parse(d)
                if(nd.code === config.status.SUCCESS){
                    // console.log("system_msg", nd.content)
                    if(cb!=null){
                        cb(nd.content)
                    }
                }   
            }
        })
    }

    //请求用户车列表
    public static requestUserCarList(cb=null) {
        http.post(config.API.user_car_list,null,(err,d)=>{
            if(!err){
                let nd = JSON.parse(d)
                if(nd.code === config.status.SUCCESS){
                    // console.log("user_car_list", nd.content)
                    if(cb!=null){
                        cb(nd.content)
                    }
                }   
            }
        },'GET')
    }

    // 请求用户注册/设置地区 stype 1:注册 2:重新设置
    public static requestUserSetRegion(region_id:number,stype:number,cb) {
        let opt = {'region_id':region_id, 'stype':stype}
        http.post(config.API.user_set_region, opt, (err,d)=>{
            let nd = JSON.parse(d)
            if(nd.code === config.status.SUCCESS){
                if(cb!=null){
                    cb(nd.content)
                }
            }  
        })
    }
    
    // 请求我的排行 stype 0:全国 1:省 2:市
    public static requestMineRank(stype:number, cb) {
        let opt = {'stype': stype}
        http.post(config.API.user_ranking, opt, (err,d)=>{
            if(!err){
                let data = JSON.parse(d)
                if(data.code===config.status.SUCCESS){
                    if(cb!=null) {
                        cb(data.content)
                    }
                }
            } else{
                console.log("user rank Data err",err)
            }
        })
    }

    // 请求排行列表
    public static requestRankList(region_id:number, cb) {
        http.get(config.STATIC_API.rankings(region_id),(err,d)=>{
            if(!err){
                let data = JSON.parse(d)
                if(data.code===config.status.SUCCESS){
                    // console.log('data=',data.content)
                    if(cb!=null) {
                        cb(data.content)
                    }
                }
            } else{
                console.log("rankList err",err)
            }
        })
    }

    // 请求tt侧边栏用户奖励
    public static requestTTSidebarUserReward(status=config.USER_TT_SIDEBAR_REWARD.GET, success_cb, fail_cb=null) {
        if(sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
            success_cb(null)
            return
        }
        if(status != config.USER_TT_SIDEBAR_REWARD.GET) {
            uiManager.Instance().showLoading()
        }
        http.post(config.API.unlock_number_status,{'stype':status}, (err,data)=>{
            if(status!= config.USER_TT_SIDEBAR_REWARD.GET) {
                uiManager.Instance().hideLoading()
            }
            if(!err) {
                let _data = JSON.parse(data)
                if(_data.code==config.status.SUCCESS) {
                    if(success_cb) {
                        success_cb(_data.content)
                    }
                } else {
                    if(fail_cb) {
                        fail_cb()
                    }
                }
            } else {
                if(fail_cb) {
                    fail_cb()
                }
            }
        })
    }

    // 请求同步数据
    public static requestSyncNumber(opt,success_cb=null,fail_cb=null) {
        http.post(config.API.sync_free_number, opt, (err,data)=>{
            if(!err) {
                let _data = JSON.parse(data)
                if(_data.code==config.status.SUCCESS) {
                    if(success_cb) {
                        success_cb(_data.content)
                    }
                } else {
                    if(fail_cb) {
                        fail_cb(_data.code)
                    }
                }
            } else {
                if(fail_cb) {
                    fail_cb(err)
                }
            }
        })
    }

    // 显示视频广告
    public static showVideoAd(ads_type=config.ADS_TYPE.UNKNOWN, success_cb, err_cb=null) {
        let ad_id = SdkUtil.getAdId(ads_type)
        SdkUtil.showVideoAd(ad_id,(res)=>{
            if(res.isEnded) {
                success_cb(res)
            } else {
                err_cb(res)
            }
        })
    }
}


