import { _decorator, ccenum, Component, Node } from 'cc';
import { FileManager } from './FileManager';
import { SaveManager } from './SaveManager';
import { SettingsData, UnlockData, UserGameData, UserLevelData } from './DataModel';
import { TopLevelManager } from '../global/data-flow/TopLevelManager';
import { MainManager } from '../global/data-flow/MainManager';

const { ccclass, property } = _decorator;

@ccclass('UserDataBehaviour')
export class UserDataBehaviour extends TopLevelManager
{
    private mainManager: MainManager;

    public fn_InitMainManager(mainManager: MainManager) 
    {
        this.mainManager = mainManager;
    }

    @property({type: FileManager})
    private m_FileManager: FileManager;
    
    @property({type: SaveManager})
    public saveManager: SaveManager;

    private userGameData: UserGameData;

    public fn_Init(): void 
    {
        this.saveManager.fn_SetIsFirstTimeUser();
        this.ShouldResetData();

        console.log("fn_IsFirstTimeUser: " + this.saveManager.fn_IsFirstTimeUser());
        if (this.saveManager.fn_IsFirstTimeUser())
        {
            this.userGameData = new UserGameData();
            this.userGameData.userLevelData = new UserLevelData();
            console.log("UserGameData: " + this.userGameData);

            this.fn_InitLevelDatas();
            this.saveManager.fn_SetGameData(this.userGameData);
        }
        else 
        {
            this.userGameData = this.saveManager.fn_LoadUserData();
            console.log("saved UserGameData: " + this.userGameData);
        }

    }

    private ShouldResetData() 
    {
        if (this.saveManager.m_DebugResetFlag)
            this.saveManager.fn_ResetData();
    }

    private fn_InitLevelDatas()
    {
        let jsonData = this.m_FileManager.fn_GetLevelDataJson();
        jsonData.forEach(data => 
        {
            this.userGameData.userLevelData.levelDatas.push(data);
        });
    }

    public fn_SetLevelDatas(levelNum: number, lastUnlockedLevel: number, unlockData: UnlockData)
    {
        this.fn_SetLevelUnlockedData(levelNum, lastUnlockedLevel);

        this.userGameData.userLevelData.levelDatas[levelNum - 1].unlockData = unlockData;
        this.saveManager.fn_SetGameData(this.userGameData);
    }

    public fn_SetLevelUnlockedData(selectedLvl: number, lastUnlockedLevel: number)
    {
        console.log("selectedLvl: " + selectedLvl);
        console.log("lastUnlockedLevel: " + lastUnlockedLevel);

        this.userGameData.userLevelData.selectedLevel = selectedLvl;
        this.userGameData.userLevelData.lastUnlockedLevel = lastUnlockedLevel;

        this.saveManager.fn_SetGameData(this.userGameData);
    }

    public fn_SetGameSettingsData(settingsData: SettingsData)
    {
        this.userGameData.settingsData = settingsData;
        this.saveManager.fn_SetGameData(this.userGameData);
    }

    public fn_GetUserData(): UserLevelData
    {
        return this.userGameData.userLevelData;
    }

    public fn_GetGameSettingData() : SettingsData
    {
        return this.userGameData.settingsData;
    }
}

