import { _decorator, Component, Node, SpriteFrame } from 'cc';
import { tools } from '../tools';
const { ccclass, property } = _decorator;

export class img_data{
    public id:number = 0;
    public url:string = "";
    constructor(id,url){
        this.id = id
        this.url = url
    }
}
@ccclass('imageCacheManager')
export class imageCacheManager  {
    private static game_car_map:Map<number,SpriteFrame> = new Map
    private static home_car_map:Map<number,SpriteFrame> = new Map
    private static rank_car_map:Map<number,SpriteFrame> = new Map
    
    public static start_cache(cb){
        let game_car_list:img_data[]=[]
        let home_car_list:img_data[]=[]
        let rank_car_list:img_data[]=[]
        let count = 3
        let num = 0
        let call_back = ()=>{
            num++;
            if(num>=count){
                cb()
            }
        }
        for (let index = 0; index < tools.all_car_list.length; index++) {
            const element = tools.all_car_list[index];
            tools.all_car_map.set(element.id,element)
            game_car_list.push(new img_data(element.id,element.cover))
            home_car_list.push(new img_data(element.id,element.home_cover))
            rank_car_list.push(new img_data(element.id,element.ranking_cover))
        }
        // console.log('tools.all_car_map=', tools.all_car_map)
        imageCacheManager.cache_game_car(game_car_list,call_back)
        imageCacheManager.cache_home_car(home_car_list,call_back)
        imageCacheManager.cache_rank_car(rank_car_list,call_back)
    }
    public static cache_game_car(list:img_data[],finish_cb){ //游戏中图
        let count = list.length
        let num = 0
        let cb = (data)=>{
            num++;
            imageCacheManager.game_car_map.set(data.tag,data.sf)
            if(num>=count){
                finish_cb()
            }
        }
        for (let index = 0; index < count; index++) {
            tools.loadRemoteImg(list[index].url,cb,list[index].id)
        }
     
    }
    public static cache_home_car(list:img_data[],finish_cb){ //	主页图
        let count = list.length
        let num = 0
        let cb = (data)=>{
            num++;
            imageCacheManager.home_car_map.set(data.tag,data.sf)
            if(num>=count){
                finish_cb()
            }
        }
        for (let index = 0; index < count; index++) {
            tools.loadRemoteImg(list[index].url,cb,list[index].id)
        }
    }
    public static cache_rank_car(list:img_data[],finish_cb){ //	排行图
        let count = list.length
        let num = 0
        let cb = (data)=>{
            num++;
            imageCacheManager.rank_car_map.set(data.tag,data.sf)
            if(num>=count){
                finish_cb()
            }
        }
        for (let index = 0; index < count; index++) {
            tools.loadRemoteImg(list[index].url,cb,list[index].id)
        }
    }

    public static getRankCarImageById(id:number){
        return imageCacheManager.rank_car_map.get(id)
    }

    public static getHomeCarImageById(id:number){
        return imageCacheManager.home_car_map.get(id)
    }

    public static getGameCarImageById(id:number){
        return imageCacheManager.game_car_map.get(id)
    }
    
}


