using System;
using LitJson;
using Wizard.RoomMatch;

namespace Wizard;

public class GatheringRule
{
	public enum eRole
	{
		NONE,
		OWNER,
		GUEST
	}

	public enum eWatchSetting
	{
		OWNER_ONLY = 1,
		ALL_MEMBER,
		NONE
	}

	public enum eType
	{
		FREE_BATTLE = 1,
		TOURNAMENT
	}

	public enum eTournamentType
	{
		NONE,
		SINGLE_ELIMINATION,
		DOUBLE_ELIMINATION
	}

	public const int DEFAULT_DECK_ENTRY_MINUTE = 5;

	public const int ID_NUMBER_OF_DIGIT = 6;

	public const int DEFAULT_MAX_MEMBER = 2;

	public const int DEFAULT_BATTLE_HOUR = 1;

	public const eWatchSetting DEFAULT_WATCH_SETTING = eWatchSetting.OWNER_ONLY;

	public bool IsReset;

	public eType Type { get; set; } = eType.FREE_BATTLE;

	public eTournamentType TournamentType { get; set; }

	public bool IsTournament
	{
		get
		{
			if (TournamentType != eTournamentType.SINGLE_ELIMINATION)
			{
				return TournamentType == eTournamentType.DOUBLE_ELIMINATION;
			}
			return true;
		}
	}

	public int MaxMember { get; set; } = 2;

	public BattleParameter BattleParameterInstance { get; private set; }

	public int BattleTimeHour { get; set; } = 1;

	public DateTime BattleStartLocalTime
	{
		get
		{
			return ConvertTime.ToLocalByDateTime(BattleStartUtcTime);
		}
		set
		{
			BattleStartUtcTime = ConvertTime.LocalToUtc(value);
		}
	}

	public DateTime BattleStartUtcTime { get; private set; }

	public bool IsOwnerEntryBattle { get; set; } = true;

	public bool IsEntryDeckOnly { get; set; }

	public eWatchSetting WatchSetting { get; set; } = eWatchSetting.OWNER_ONLY;

	public string GetGatheringTypeString()
	{
		return GatheringUtility.GetGatheringTypeString(Type, TournamentType, IsReset);
	}

	public string GetGatheringResetString()
	{
		if (!IsReset)
		{
			return "仮リセットなし";
		}
		return "仮リセットあり";
	}

	public GatheringRule()
	{
	}

	public GatheringRule(JsonData data, JsonData config)
	{
		BattleParameterInstance = BattleParameter.JsonToBattleParameter(data);
		MaxMember = data["join_member_limit"].ToInt();
		IsOwnerEntryBattle = data["is_master_joined"].ToInt() == 1;
		WatchSetting = (eWatchSetting)data["watch_type"].ToInt();
		Type = (eType)data["gathering_type"].ToInt();
		if (Type == eType.TOURNAMENT)
		{
			IsEntryDeckOnly = true;
		}
		else if (config.Keys.Contains("is_deck_entry"))
		{
			IsEntryDeckOnly = config["is_deck_entry"].ToInt() == 1;
		}
		if (config.TryGetValue("format", out var value))
		{
			TournamentType = (eTournamentType)value.ToInt();
		}
		if (config.TryGetValue("is_reset", out var value2))
		{
			IsReset = value2.ToInt() == 1;
		}
	}

	public void SaveToPlayerPrefs()
	{
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.GATHERING_MAX_MEMBER, MaxMember);
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.GATHERING_FORMAT, (int)BattleParameterInstance.DeckFormat);
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.GATHERING_BATTLE_STYLE, (int)BattleParameterInstance.Rule);
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.GATHERING_BATTLE_TYPE, (int)Type);
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.GATHERING_BATTLE_TORNAMENT_TYPE, (int)TournamentType);
		PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.GATHERING_IS_RESET, IsReset);
		PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.GATHERING_OWNER_ENTRY_BATTLE, IsOwnerEntryBattle);
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.GATHERING_WATCH_SETTING, (int)WatchSetting);
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.GATHERING_BATTLE_HOUR, BattleTimeHour);
		PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.GATHERING_IS_ENTRY_DECK_ONLY, IsEntryDeckOnly);
	}

	public void LoadFromPlayerPrefs()
	{
		MaxMember = PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.GATHERING_MAX_MEMBER);
		Format format = (Format)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.GATHERING_FORMAT);
		if (format == Format.PreRotation && Prerelease.Status != Prerelease.eStatus.PRE_ROTATION)
		{
			format = Format.Rotation;
		}
		if (format == Format.Crossover && (!Data.Crossover.IsWithinGatheringPeriod || Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.ROOM_FORMAT_CROSSOVER)))
		{
			format = Format.Rotation;
		}
		else if (format == Format.MyRotation && (!Data.MyRotationAllInfo.IsWithinGatheringPeriod || Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.GATHERING_MYROTATION)))
		{
			format = Format.Rotation;
		}
		else if (format == Format.Avatar && (!Data.AvatarBattleAllInfo.IsWithinGatheringPeriod || Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.GATHERING_AVATAR)))
		{
			format = Format.Rotation;
		}
		BattleParameterInstance = new BattleParameter(NetworkDefine.ServerBattleType.Gathering, format, TwoPickFormat.None, (RoomConnectController.BattleRule)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.GATHERING_BATTLE_STYLE), isOpenDeckRoom: false);
		IsOwnerEntryBattle = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.GATHERING_OWNER_ENTRY_BATTLE);
		WatchSetting = (eWatchSetting)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.GATHERING_WATCH_SETTING);
		BattleTimeHour = PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.GATHERING_BATTLE_HOUR);
		IsEntryDeckOnly = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.GATHERING_IS_ENTRY_DECK_ONLY);
		Type = (eType)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.GATHERING_BATTLE_TYPE);
		TournamentType = (eTournamentType)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.GATHERING_BATTLE_TORNAMENT_TYPE);
		IsReset = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.GATHERING_IS_RESET);
	}
}
