import { _decorator, sys } from 'cc';
import { user_car_list, user_free_ads_data, userData } from '../data';
import { config } from '../config';
import { tools } from '../tools';
import { GameManager } from '../GameManager';
const { ccclass, property } = _decorator;

@ccclass('userDataManager')
export class userDataManager  {
    public static user_data:userData = null
    public static user_car_list:user_car_list = null
    public static free_ads_data:user_free_ads_data = null

    // 今天分享相关
    public static isTodayCanShare():boolean {
        if(userDataManager.user_data.share_number>=tools.sys_config.share_number) {
            return false
        }
        return true
    }

    public static addTodayShareNumber() {
        if(userDataManager.user_data.share_number>=tools.sys_config.share_number) {
            return 
        }
        userDataManager.user_data.share_number+=1
        // console.log('同步接口  今天分享数:',userDataManager.user_data.share_number)
        let opt = {'share_number':userDataManager.user_data.share_number}
        GameManager.requestSyncNumber(opt)
    }

    // 每天免费玩游戏
    public static isTodayCanFreePlayGame():boolean {
        if(userDataManager.user_data.free_game_number>=tools.sys_config.free_game_number) {
            return false
        }
        return true
    }

    public static addTodayPlayGameNumber() {
        if(userDataManager.user_data.free_game_number>=tools.sys_config.free_game_number) {
            return
        }
        userDataManager.user_data.free_game_number+=1
        // console.log('同步接口  今天玩游戏数:',userDataManager.user_data.free_game_number)
        let opt = {'free_game_number':userDataManager.user_data.free_game_number}
        GameManager.requestSyncNumber(opt)
    }

    // 用户免广告相关
    public static getUserIsFreeAds():boolean {
        return userDataManager.free_ads_data.is_free
    }

    public static saveUserFreeAdsData() {
        let data = JSON.stringify(userDataManager.free_ads_data)
        // console.log('保存-----用户免费广告',data)
        sys.localStorage.setItem(config.LOCAL_STORAGE.USER_FREE_ADS_DATA, data)
    }

    public static clearUserFreeAdsData() {
        sys.localStorage.removeItem(config.LOCAL_STORAGE.USER_FREE_ADS_DATA)
    }

    public static getUserFreeAdsData():user_free_ads_data {
        if(userDataManager.free_ads_data!=null) {
            // console.log('获取--静态---用户免费广告',userDataManager.free_ads_data)
            return userDataManager.free_ads_data
        }
        let str = sys.localStorage.getItem(config.LOCAL_STORAGE.USER_FREE_ADS_DATA)
        // console.log('str=',str)
        if(str==undefined||str==""||str==null||str=='null'){
            userDataManager.free_ads_data = new user_free_ads_data
        } else {
            let nd:user_free_ads_data = JSON.parse(str)
            userDataManager.free_ads_data = nd            
        }
        // console.log('获取--解析---用户免费广告',userDataManager.free_ads_data)
        return userDataManager.free_ads_data
    }

    public static addUserFreeAdsSeconds(add_seconds:number) {
        let free_ads_data = userDataManager.getUserFreeAdsData()
        if(free_ads_data.start_date_time<=0) {
            let cur_time = new Date().getTime()
            free_ads_data.start_date_time = cur_time
        }
        free_ads_data.start_date_time = free_ads_data.start_date_time + add_seconds * 1000
        userDataManager.saveUserFreeAdsData()
    }
    
    public static getUserFreeAdsSeconds() {
        let free_ads_data = userDataManager.getUserFreeAdsData()
        let old_time = free_ads_data.start_date_time
        // console.log('old_time=',old_time)
        if(old_time<=0) {
            free_ads_data.is_free = false
            free_ads_data.is_look_video_infinite_count_reward = false
            return old_time
        }
        
        let free_seconds = free_ads_data.is_look_video_infinite_count_reward ? tools.sys_config.free_game_time : 0
        let cur_time = new Date() 
        let time_diff = cur_time.getTime() - old_time
        let seconds = Math.floor(time_diff / 1000);
        let shengyu_seconds = free_seconds - seconds
        // console.log('上次时间距离当前秒:',seconds,'shengyu_seconds=',shengyu_seconds)
        if(shengyu_seconds<=0) {
            free_ads_data.is_free = false
            return 0
        }
        free_ads_data.is_free = true
        return shengyu_seconds
    }
}


