using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Game.Challenge;
using Spine.Collections;
using UnityEngine;
using XCrew;
//using Event = XCrew.Event;
[Serializable]
public enum ChallengeTutorial
{
Locked,
Unlocked,
Done,
}
public class ChallengeSave : UserData
{
public static ChallengeSave Ins => LocalData.Get<ChallengeSave>();
// private Dictionary<ChallengeDifficulty, Dictionary<int, ChallengeSaveData>> Data =
// new Dictionary<ChallengeDifficulty, Dictionary<int, ChallengeSaveData>>();
public Dictionary<int, ChallengeSaveData> DataNormalChallenge = new Dictionary<int, ChallengeSaveData>();
public Dictionary<int, ChallengeSaveData> DataHardChallenge = new Dictionary<int, ChallengeSaveData>();
public ChallengeTutorial _ChallengeTutorial = ChallengeTutorial.Locked;
public ChallengeSaveData GetOrCreateChallengeSaveData(ChallengeDifficulty difficulty, int level)
{
var challengeSaveList = GetChallengeSaveList(difficulty);
//- if level not exist, create it
if (!challengeSaveList.ContainsKey(level))
{
challengeSaveList.Add(level, new ChallengeSaveData(difficulty, level));
}
Save();
return challengeSaveList[level];
}
/// <summary>
/// Can be NULL
/// </summary>
public ChallengeSaveData GetChallengeSaveData(ChallengeDifficulty difficulty, int level)
{
var challengeSaveList = GetChallengeSaveList(difficulty);
if (challengeSaveList.ContainsKey(level))
return challengeSaveList[level];
return null;
}
public Dictionary<int, ChallengeSaveData> GetChallengeSaveList(ChallengeDifficulty difficulty)
{
//- if difficulty not exist, create it
if (difficulty == ChallengeDifficulty.Normal)
{
if (DataNormalChallenge == null)
{
DataNormalChallenge = new Dictionary<int, ChallengeSaveData>();
Save();
}
return DataNormalChallenge;
}
else //- hard
{
if (DataHardChallenge == null)
{
DataHardChallenge = new Dictionary<int, ChallengeSaveData>();
Save();
}
return DataHardChallenge;
}
}
public int NumOfGames(ChallengeDifficulty difficulty, State state)
{
if (difficulty == ChallengeDifficulty.Normal)
{
if (DataNormalChallenge == null)
{
return 0;
}
return DataNormalChallenge.Count(save => save.Value.state == state);
}
else //- hard
{
if (DataHardChallenge == null)
{
return 0;
}
return DataHardChallenge.Count(save => save.Value.state == state);
}
}
public int GetStartLevelToShowNineItems(ChallengeDifficulty difficulty)
{
//- old, search 0 -> end
// var normalChallengeSaves = GetChallengeSaveList(ChallengeDifficulty.Normal);
// var temp = normalChallengeSaves.ToList();
// temp.Sort((a, b) => a.Key.CompareTo(b.Key));
//
// for (int i = 0; i < temp.Count; i += 9)
// {
// if(temp[i].Value.state == State.Win)
// for (int j = 0; j < 9; ++j)
// {
// if (temp[i + j].Value.state != State.Win)
// {
// return i;
// }
// }
// }
try
{
if (difficulty == ChallengeDifficulty.Normal)
{
var challengeNormalGroupDone = UserProfileData.Ins.ChallengeNormalGroupDone;
var startLevelToCheck = challengeNormalGroupDone * 9;
var endLevelToCheck = startLevelToCheck + 8; //- 9 levels
for (int i = startLevelToCheck; i <= endLevelToCheck; ++i)
{
var challengeSave = GetChallengeSaveData(ChallengeDifficulty.Normal, level: i);
if (challengeSave == null)
{
return startLevelToCheck;
}
if (challengeSave.state != State.Win)
{
return startLevelToCheck;
}
}
UserProfileData.Ins.ChallengeNormalGroupDone += 1;
Save();
return UserProfileData.Ins.ChallengeNormalGroupDone * 9;
}
else //- hard
{
var challengeHardGroupDone = UserProfileData.Ins.ChallengeHardGroupDone;
var startLevelToCheck = challengeHardGroupDone * 9;
var endLevelToCheck = startLevelToCheck + 8; //- 9 levels
for (int i = startLevelToCheck; i <= endLevelToCheck; ++i)
{
var challengeSave = GetChallengeSaveData(ChallengeDifficulty.Hard, level: i);
if (challengeSave == null)
{
return startLevelToCheck;
}
if (challengeSave.state != State.Win)
{
return startLevelToCheck;
}
}
UserProfileData.Ins.ChallengeHardGroupDone += 1;
Save();
return UserProfileData.Ins.ChallengeHardGroupDone * 9;
}
}
catch (Exception e)
{
Debug.LogError($"[Challenge] SOS! {e}");
return -1;
}
}
public void Init()
{
/*var normalChallengeData = ChallengeController.Ins.GetChallengeGroupData(ChallengeDifficulty.Normal);
for (int level = 0; level < normalChallengeData.challengeData.Count; ++level)
{
var save = GetChallengeSaveData(ChallengeDifficulty.Normal, level);
if (save == null)//- not have save
{
GetChallengeSaveList(ChallengeDifficulty.Normal).Add(level, new ChallengeSaveData());
}
}*/
}
public void Save(State state, ChallengeDifficulty difficulty, int level)
{
var challenge = GetOrCreateChallengeSaveData(difficulty, level);
//---- if last state is win, play again get lose -> dont save state
if (state == State.Lose && challenge.state == State.Win)
{
return;
}
challenge.state = state;
if (state == State.Win)
{
++challenge.NumOfWins;
}
Save();
var challengeItem = ChallengePanel.GetChallengeItem(difficulty, level);
challengeItem?.UpdateState();
}
public void Seen(ChallengeDifficulty difficulty, int level)
{
var challenge = GetOrCreateChallengeSaveData(difficulty, level);
challenge.HasSeen = true;
Save();
}
public void CheckDoneTutorial()
{
if (_ChallengeTutorial != ChallengeTutorial.Unlocked)
return;
_ChallengeTutorial = ChallengeTutorial.Done;
//GameUIController.Ins.TutorialDialogChallenge.ChallengeTutorial_OnEnd();
Save();
}
public bool IsTutorialChallenge()
{
if (_ChallengeTutorial == ChallengeTutorial.Done)
return false;
var levelClassicNeedToUnlock =
ChallengeController.GetLevelRequiredToUnlock(ChallengeDifficulty.Normal, 0); //- get first
return UserProfileData.Ins.CurrentlevelClassic == levelClassicNeedToUnlock;
}
public bool CheckUnlockTutorial()
{
switch (_ChallengeTutorial)
{
case ChallengeTutorial.Unlocked:
{
Debug.Log($"[debug] add ChallengeTutorial_Show");
/*UIDialogController.Instance.ActionOnEndStackDialog +=
GameUIController.Ins.TutorialDialogChallenge.ChallengeTutorial_Show;*/
return true;
}
case ChallengeTutorial.Locked:
{
var levelClassicNeedToUnlock =
ChallengeController.GetLevelRequiredToUnlock(ChallengeDifficulty.Normal, 0); //- get first
if(IsTutorialChallenge())
{
_ChallengeTutorial = ChallengeTutorial.Unlocked;
Save();
goto case ChallengeTutorial.Unlocked;
}
else
{
//GameUIController.Ins.TutorialDialogChallenge.Hide();
return false;
}
// GameUIController.Ins.TutorialDialogChallenge.ChallengeTutorial_OnEnd();
break;
}
case ChallengeTutorial.Done:
{
//GameUIController.Ins.TutorialDialogChallenge.Hide();
return false;
}
}
return false;
}
}
[Serializable]
public class ChallengeSaveData
{
public State state = State.LockedByLevel;
public int Level;
public ChallengeDifficulty difficulty;
public int NumOfWins = 0;
/// <summary>
/// Use for tracking new challenge has unlocked by level then notify on UI
/// </summary>
public bool HasSeen = false;
public ChallengeSaveData(ChallengeDifficulty difficulty, int level)
{
Level = level;
this.difficulty = difficulty;
}
public void Win()
{
}
public void Lose()
{
}
}
//
// [Serializable]
// public class NormalSaveData : ChallengeSaveData
// {
//
// }
//
// [Serializable]
// public class HardSaveData : ChallengeSaveData
// {
// }