using Cute;
using UnityEngine;
using Wizard;
using Wizard.Scripts.Network.Task.Arena;
using Wizard.Scripts.Network.Task.Arena.TwoPick;

public class TaskManager : MonoBehaviour
{
	private NetworkManager networkManager;

	private GameObject returnSendObj;

	private static TaskManager main;

	private string returnMsg = "";

	private const string mypageSendTraceFlag_Key = "mypageSendTraceFlag";

	private void Awake()
	{
		main = this;
	}

	public static TaskManager GetInstance()
	{
		return main;
	}

	private NetworkManager getNetworkManager()
	{
		if (networkManager == null)
		{
			networkManager = Toolbox.NetworkManager;
		}
		return networkManager;
	}

	public void StartMailTop(GameObject sendObj, string msg, bool isTutorial)
	{
		returnMsg = msg;
		returnSendObj = sendObj;
		MailTopTask mailTopTask = new MailTopTask();
		mailTopTask.SetParameter(isTutorial);
		StartCoroutine(getNetworkManager().Connect(mailTopTask, mailtop, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode));
	}

	private void mailtop(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	private void ReadMail(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	private void UserData(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	public void StartArenaTopTaskData(GameObject sendObj, string msg, int mode)
	{
		returnMsg = msg;
		returnSendObj = sendObj;
		ArenaTopTaskBase taskInstance = ArenaTopTaskBase.GetTaskInstance(mode);
		taskInstance.SetParameter();
		StartCoroutine(getNetworkManager().Connect(taskInstance, ArenaTopTaskData, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode));
	}

	private void ArenaTopTaskData(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	public void StartArenaPrepareData(GameObject sendObj, string msg, int mode, int payType)
	{
		returnMsg = msg;
		returnSendObj = sendObj;
		ArenaEntryTaskBase taskInstance = ArenaEntryTaskBase.GetTaskInstance(mode);
		taskInstance.SetParameter(payType);
		StartCoroutine(getNetworkManager().Connect(taskInstance, ArenaPrepareData, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode));
	}

	private void ArenaPrepareData(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	public void StartArenaClassCharaChooseData(GameObject sendObj, string msg, int classId)
	{
		ClassCharaChooseTask classCharaChooseTask = new ClassCharaChooseTask();
		classCharaChooseTask.SetParameter(classId);
		StartTask(classCharaChooseTask, sendObj, msg);
	}

	public void StartTask(NetworkTask task, GameObject sendObj, string msg)
	{
		returnMsg = msg;
		returnSendObj = sendObj;
		StartCoroutine(getNetworkManager().Connect(task, SendMessage, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode));
	}

	public void StartArenaCardChooseTaskData(GameObject sendObj, string msg, int selectedId)
	{
		CardChooseTask cardChooseTask = new CardChooseTask();
		cardChooseTask.SetParameter(selectedId);
		StartTask(cardChooseTask, sendObj, msg);
	}

	private void SendMessage(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	public void StartArenaRetireTaskData(GameObject sendObj, string msg, int mode)
	{
		returnMsg = msg;
		returnSendObj = sendObj;
		ArenaRetireTaskBase taskInstance = ArenaRetireTaskBase.GetTaskInstance(mode);
		taskInstance.SetParameter();
		StartCoroutine(getNetworkManager().Connect(taskInstance, ArenaRetireTaskData, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode));
	}

	private void ArenaRetireTaskData(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	public void StartArenaFinishTaskData(GameObject sendObj, string msg, int mode)
	{
		returnMsg = msg;
		returnSendObj = sendObj;
		FinishTask finishTask = new FinishTask();
		finishTask.SetParameter();
		StartCoroutine(getNetworkManager().Connect(finishTask, ArenaFinishTaskData, BaseTask.OnRequestFailed, BaseTask.OnFailedErrorCode));
	}

	private void ArenaFinishTaskData(NetworkTask.ResultCode error)
	{
		returnSendObj.SendMessage(returnMsg);
	}

	public static void GetErrorMsgFromCode(int code, out string msg, out string title)
	{
		string key = "Error_" + code.ToString("0000");
		string key2 = "ErrorHeader_" + code.ToString("0000");
		msg = Data.SystemText.Get(key);
		title = Data.SystemText.Get(key2);
	}

	public void ClearLastLogKey()
	{
		LocalLog.ClearLastLogKey();
		NotMyPageSend();
	}

	public void MyPageSend()
	{
		PlayerPrefs.SetInt("mypageSendTraceFlag", 1);
	}

	public void NotMyPageSend()
	{
		PlayerPrefs.SetInt("mypageSendTraceFlag", 0);
	}

	public bool IsMyPageSend()
	{
		if (PlayerPrefs.GetInt("mypageSendTraceFlag") != 1)
		{
			return false;
		}
		return true;
	}

	private void BackToHome()
	{
		GameMgr.GetIns().GetBattleCtrl().BattleEnd(UIManager.ViewScene.MyPage);
	}
}
