import { _decorator, assetManager, Component, ImageAsset, instantiate, Node, Prefab, resources, SpriteFrame, Texture2D } from 'cc';
import { config } from './config';

import { main } from './main';
import { car_item_data, edit_game_config_data, model_item_data, userData } from './data';
import { game } from './game/game';
import { Util } from './util';
import { http } from './http';
const { ccclass, property } = _decorator;

@ccclass('tools')
export class tools  {
    public static levels:string[] = []
    public static tpl_list:model_item_data[] = []
    public static game_config:edit_game_config_data = null
    public static all_car_list:car_item_data[] =[]
    public static init(n:Node){
        tools.parent = n
    }
    public static playGame(call_back){
        resources.load(config.PREFAB.game,(err,prefab:Prefab)=>{
            let node = instantiate(prefab)
            node.parent = tools.parent
            node.getComponent(game).show(call_back)
        })
    }
    public static parent:Node = null;

    public static getBoxRandom(type:number){
        for (let index = 0; index < tools.game_config.box_list.length; index++) {
            const element =  tools.game_config.box_list[index];
            if(element.type==type){
                return element
            }
        }
    }
    public static isBox(type:number){
        if(type<=5||type>=9){
            return true
        }
        return false
    }

    public static randomCoin(){
       return Util.getRandomInt(tools.game_config.scores.min,tools.game_config.scores.max) 
    }

    public static loadRemoteImg(url:string,call,tag:number=-1){
        if(url==undefined) { return null }
        if(url.length<=0){ return null }
        assetManager.loadRemote<ImageAsset>(url, (err, imageAsset2)=>{
            if (!err && imageAsset2) {        
                const texture = new Texture2D();
                texture.image = imageAsset2;
                let spFrame2 = new SpriteFrame();     
                spFrame2.texture = texture;
                spFrame2.addRef()
                if(call) {
                    call({"url":url,"sf":spFrame2,"tag":tag})
                }
            }
        }); 
    }

    // 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)
                    }
                }  
        })
    }

    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)
            }
        })
        // let opt = {'stype': stype}
        // http.post(config.API.user_ranking, opt, (err,d)=>{
        //     if(!err){
        //         console.log('排行=',d)
        //         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)
        //     }
        // })
    }
}


