import { _decorator, Component, Node, SpriteFrame, sys } from 'cc';
import { tools } from '../tools';
import { userDataManager } from './userDataManager';
import { imageCacheManager } from './imageCacheManager';
import { car_item_attribute_data, car_item_data } from '../data';
import { config } from '../config';
import { SdkUtil } from '../sdkUtil';
const { ccclass, property } = _decorator;

@ccclass('dataManager')
export class dataManager  {
  
     // 获取平台标识
    public static getPlatformId() {
      let platform_id = config.Platform_id.browser
      if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
          platform_id =  config.Platform_id.tt
      }
      else if (sys.platform == sys.Platform.WECHAT_GAME) {
        if(SdkUtil.KS_GAME) {
          platform_id =  config.Platform_id.ks
        } else {
          platform_id =  config.Platform_id.wx
        }
      }
      return platform_id
    }

    // 获取用户默认首页车sf
    public static getUserDefaultHomeCarSf():SpriteFrame {
      let sf = imageCacheManager.getHomeCarImageById(userDataManager.user_car_list.default_car_id)
      return sf
    }

    // 获取用户默认排行车sf
    public static getUserDefaultRankCarSf():SpriteFrame {
      let sf = imageCacheManager.getRankCarImageById(userDataManager.user_car_list.default_car_id)
      return sf
    }

    // 获取用户排行车sf
    public static getUserRankCarSf(car_id:number, user_id:number):SpriteFrame {
      if(userDataManager.user_data.id == user_id) {
        return dataManager.getUserDefaultRankCarSf()
      } else {
        return imageCacheManager.getRankCarImageById(car_id)
      }
    }

    // 获取用户车信息
    public static getUserCarInfo(car_id:number):car_item_data {
      let car_item:car_item_data = tools.all_car_map.get(car_id)
        if(car_item) {
          if(car_item.attribute==undefined||car_item.attribute==null) {
            car_item.attribute = new car_item_attribute_data()
          }
        } else {
          car_item = null
        }
        return car_item
    }

    // 用户列车表中有车
    public static userCarListHavCar(car_id:number):boolean {
      let on_car = false
      for (let index = 0; index < userDataManager.user_car_list.car_list.length; index++) {
        const element = userDataManager.user_car_list.car_list[index];
        if(element==car_id) {
          on_car = true
          break
        }
      }
      return on_car
    }

    // 获取游戏结束提示索引
    public static getGameEndPrompt(score:number):number {
      let cur_index = -1
      let list = tools.sys_config.game_end_prompt
      for (let index = 0; index < list.length; index++) {
          const element = list[index];
          if(typeof element === "string") {
              let arr = element.split('_')
              if(arr.length == 2) {
                  let start_num = Number(arr[0])
                  let end_num = Number(arr[1])
                  // console.log('start_num=',start_num,'end_num=',end_num)
                  if(list.length-1==index) { //最后一个只判断开始值
                      if(score>=start_num) {
                          cur_index = index
                          break
                      }
                  } else {
                      if(score>=start_num&&score<=end_num) {
                          cur_index = index
                          break
                      }
                  }
              }
          }
      }
      // console.log('cur_index=',cur_index)
      return cur_index
  }

}


