import { _decorator, assetManager, Component, ImageAsset, instantiate, Label, Node, Prefab, resources, SpriteFrame, sys, Texture2D } from 'cc';
import { config } from './config';
import { car_item_data, edit_game_config_data, model_item_data, rankData, sysConfig, user_red_dot_data, userData } from './data';
import { game } from './game/game';
import { Util } from './util';
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 sys_config:sysConfig = null
    public static mine_rank_data:rankData = null
    public static all_car_list:car_item_data[] =[]
    public static all_car_map:Map<number,car_item_data> = new Map
    public static all_car_page_list = []
    public static user_red_dot_data:user_red_dot_data = new user_red_dot_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})
                }
            }
        }); 
    }

    public static labelCutString(node:Node, str:string, length:number, is_text_center:boolean = true) {
        let lab_component = node.getComponent(Label)
        if(lab_component==null) { return }
        if(str.length>length) {
            lab_component.string = str.substring(0,length) + '...'
            lab_component.horizontalAlign = Label.HorizontalAlign.LEFT
        } else {
            lab_component.string = str
            if(is_text_center) {
                lab_component.horizontalAlign = Label.HorizontalAlign.CENTER
            }
        }
    }

    public static substringRankRegionName(node:Node,string:string) {
        if(string==undefined||string==null) { return "" }
        let lab_component = node.getComponent(Label)
        if(lab_component==null) { return }
        if(string.length>4) {
            string = string.substring(0,4)
        }
        lab_component.fontSize = 27
        if(string.length==2) {
            lab_component.lineHeight = 55
        } else if(string.length==3) {
            lab_component.lineHeight = 35
        } else {
            lab_component.lineHeight = 27
        }
        lab_component.string = string
    }

}


