using System;
using System.Collections.Generic;
using System.Globalization;
using script;
using UnityEngine;

public class ADVKagManager : BaseKagManager
{
	public ADVKagManager(TJSScript tjs, ScriptManager script_mgr, MessageWindowMgr message_mgr) : base(tjs, script_mgr)
	{
		this.message_mgr_ = message_mgr;
		this.update_count_ = 0UL;
	}

	public override void Initialize()
	{
		NDebug.AssertNull(this.message_mgr_ != null);
		base.Initialize();
		this.kag_.AddTagCallBack("scenecall", new KagScript.KagTagCallBack(this.TagSceneCall));
		this.kag_.AddTagCallBack("sceneunload", new KagScript.KagTagCallBack(this.TagSceneUnload));
		this.kag_.AddTagCallBack("talk", new KagScript.KagTagCallBack(this.TagTalk));
		this.kag_.AddTagCallBack("hitret", new KagScript.KagTagCallBack(this.TagHitRet));
		this.kag_.AddTagCallBack("messagewindow", new KagScript.KagTagCallBack(this.TagMessageWindow));
		this.kag_.AddTagCallBack("fade", new KagScript.KagTagCallBack(this.TagFade));
		this.kag_.AddTagCallBack("choicesset", new KagScript.KagTagCallBack(this.TagChoicesSet));
		this.kag_.AddTagCallBack("choicesrandomset", new KagScript.KagTagCallBack(this.TagChoicesRandomSet));
		this.kag_.AddTagCallBack("choicesrandomselect", new KagScript.KagTagCallBack(this.TagChoicesRandomSelect));
		this.kag_.AddTagCallBack("calldialog", new KagScript.KagTagCallBack(this.TagCallDialog));
		this.kag_.AddTagCallBack("_choicesshow", new KagScript.KagTagCallBack(this.Tag_ChoicesShow));
		this.kag_.LoadScenarioString("@macro name='choicesshow'\n\t@_choicesshow\n\t@s\n\t@endmacro\n");
		this.kag_.Exec();
	}

	public override void Update()
	{
		base.Update();
		if (this.vr_commu_mode)
		{
			return;
		}
		if (!GameMain.Instance.SysDlg.IsDecided)
		{
			return;
		}
		if (this.skip_mode && this.update_count_ % 2UL == 0UL && this.message_mgr_.IsClickAction())
		{
			if (!GameMain.Instance.CMSystem.MsgVoiceNoStop)
			{
				GameMain.Instance.SoundMgr.VoiceStopAll();
			}
			this.Exec();
		}
		else if (this.auto_mode)
		{
			int exitWaitCount = this.message_mgr_.GetExitWaitCount();
			bool flag = GameMain.Instance.SoundMgr.isVoicePlaying();
			if (!GameMain.Instance.CMSystem.MsgVoiceNoStop)
			{
				flag = false;
			}
			if (this.auto_wait_count <= exitWaitCount && this.message_mgr_.IsClickAction() && !flag)
			{
				if (!GameMain.Instance.CMSystem.MsgVoiceNoStop)
				{
					GameMain.Instance.SoundMgr.VoiceStopAll();
				}
				this.Exec();
			}
		}
		this.update_count_ += 1UL;
	}

	public void OnClickEvent()
	{
		if (this.GetSettingSkipMode())
		{
			this.message_mgr_.CallEvent(MessageWindowMgr.MessageWindowUnderButton.Skip);
			return;
		}
		if (!GameMain.Instance.CMSystem.MsgVoiceNoStop)
		{
			GameMain.Instance.SoundMgr.VoiceStopAll();
		}
		this.Exec();
	}

	public override bool Exec()
	{
		base.ClearExecWait();
		if (this.IsExec() && this.message_mgr_.IsChAnimeEnabled())
		{
			this.message_mgr_.FinishChAnime();
			return true;
		}
		return base.Exec();
	}

	public bool TagSceneCall(KagTagSupport tag_data)
	{
		base.CheckAbsolutelyNecessaryTag(tag_data, "scenecall", new string[]
		{
			"name"
		});
		this.tag_backup_ = tag_data.GetTagList();
		if (tag_data.IsValid("add"))
		{
			GameMain.Instance.AddScene(tag_data.GetTagProperty("name").AsString());
		}
		else
		{
			this.script_mgr_.StopMotionScript();
			this.script_mgr_.is_motion_blend = true;
			this.message_mgr_.CloseMessageWindowPanel();
			this.message_mgr_.SetMessageViewerActive(true);
			this.message_mgr_.CancelSkipAndAuto();
			GameMain.Instance.LoadScene(tag_data.GetTagProperty("name").AsString());
		}
		if (tag_data.IsValid("enabled"))
		{
			base.enabled = tag_data.GetTagProperty("enabled").AsBool();
		}
		return true;
	}

	public bool TagSceneUnload(KagTagSupport tag_data)
	{
		if (tag_data.IsValid("name"))
		{
			GameMain.Instance.UnloadScene(tag_data.GetTagProperty("name").AsString());
		}
		else
		{
			GameMain.Instance.UnloadPopScene();
		}
		return false;
	}

	public bool TagTalk(KagTagSupport tag_data)
	{
		this.message_mgr_.OpenMessageWindowPanel();
		this.text_data_.talk_name = string.Empty;
		if (tag_data.IsValid("name"))
		{
			this.text_data_.talk_name = ScriptManager.ReplaceCharaName(tag_data.GetTagProperty("name").AsString());
		}
		this.text_data_.vice_name = string.Empty;
		if (tag_data.IsValid("real"))
		{
			this.text_data_.vice_name = tag_data.GetTagProperty("real").AsString();
		}
		this.text_data_.voice_file = string.Empty;
		if (tag_data.IsValid("voice"))
		{
			this.text_data_.voice_maid = BaseKagManager.GetVoiceTargetMaid(tag_data);
			if (this.text_data_.voice_maid != null)
			{
				this.text_data_.voice_maid.LipSyncEnabled(!tag_data.IsValid("np"));
			}
			this.text_data_.voice_file = tag_data.GetTagProperty("voice").AsString() + ".ogg";
		}
		return false;
	}

	public bool TagHitRet(KagTagSupport tag_data)
	{
		this.text_data_.text = this.kag_.GetText();
		this.kag_.TextClear();
		this.text_data_.text = ScriptManager.ReplaceCharaName(this.text_data_.text);
		StringInfo stringInfo = new StringInfo(this.text_data_.text);
		int lengthInTextElements = stringInfo.LengthInTextElements;
		int num = lengthInTextElements - 1;
		while (0 < num)
		{
			string value = stringInfo.SubstringByTextElements(num, 1);
			if (0 > "| \u3000".IndexOf(value))
			{
				break;
			}
			this.text_data_.text = stringInfo.SubstringByTextElements(0, num);
			num--;
		}
		this.text_data_.text = this.text_data_.text.Replace('|', '\n');
		int voice_pitch = 50;
		if (this.text_data_.voice_maid != null)
		{
			voice_pitch = this.text_data_.voice_maid.VoicePitch;
		}
		this.message_mgr_.SetText(this.text_data_.talk_name, this.text_data_.text, this.text_data_.voice_file, voice_pitch);
		if (!string.IsNullOrEmpty(this.text_data_.voice_file))
		{
			GameMain.Instance.SoundMgr.VoiceStopAll();
			if (!this.script_mgr_.adv_kag.skip_mode)
			{
				if (this.text_data_.voice_maid != null && this.text_data_.voice_maid.Visible)
				{
					this.text_data_.voice_maid.AudioMan.LoadPlay(this.text_data_.voice_file, 0f, false, false);
				}
				else
				{
					GameMain.Instance.SoundMgr.PlayDummyVoice(this.text_data_.voice_file, 0f, false, false, voice_pitch);
				}
			}
			this.text_data_.voice_maid = null;
		}
		return true;
	}

	public bool TagMessageWindow(KagTagSupport tag_data)
	{
		if (tag_data.IsValid("on"))
		{
			this.message_mgr_.OpenMessageWindowPanel();
		}
		else if (tag_data.IsValid("off"))
		{
			this.message_mgr_.CloseMessageWindowPanel();
		}
		return false;
	}

	public bool TagFade(KagTagSupport tag_data)
	{
		base.CheckAbsolutelyNecessaryTag(tag_data, "fade", new string[]
		{
			"time"
		});
		int millisecond = tag_data.GetTagProperty("time").AsInteger();
		float f_fTime = GameUty.MillisecondToSecond(millisecond);
		bool flag = tag_data.IsValid("sync");
		bool flag2 = tag_data.IsValid("color");
		Color black = Color.black;
		if (flag2)
		{
			string[] array = tag_data.GetTagProperty("color").AsString().Split(new char[]
			{
				':'
			});
			black.r = float.Parse(array[0]) / 255f;
			black.g = float.Parse(array[1]) / 255f;
			black.b = float.Parse(array[2]) / 255f;
		}
		if (tag_data.IsValid("in"))
		{
			this.script_mgr_.is_motion_blend = true;
			GameMain.Instance.MainCamera.FadeIn(f_fTime, false, null, true, !flag2, black);
		}
		else
		{
			this.script_mgr_.is_motion_blend = false;
			GameMain.Instance.MainCamera.FadeOut(f_fTime, false, null, true, black);
		}
		this.message_mgr_.ClearText();
		if (flag)
		{
			base.SetFadeWait(true);
		}
		return flag;
	}

	public bool TagChoicesSet(KagTagSupport tag_data)
	{
		base.CheckAbsolutelyNecessaryTag(tag_data, "choicesset", new string[]
		{
			"text",
			"label"
		});
		string key = this.text_data_.text = ScriptManager.ReplaceCharaName(tag_data.GetTagProperty("text").AsString());
		bool value = true;
		if (tag_data.IsValid("enabled"))
		{
			value = tag_data.GetTagProperty("enabled").AsBool();
		}
		this.choise_data_.Add(new KeyValuePair<string, KeyValuePair<string, bool>>(key, new KeyValuePair<string, bool>(tag_data.GetTagProperty("label").AsString(), value)));
		return false;
	}

	public bool TagChoicesRandomSet(KagTagSupport tag_data)
	{
		base.CheckAbsolutelyNecessaryTag(tag_data, "choicesrandomse", new string[]
		{
			"text",
			"label"
		});
		string key = this.text_data_.text = ScriptManager.ReplaceCharaName(tag_data.GetTagProperty("text").AsString());
		bool value = true;
		if (tag_data.IsValid("enabled"))
		{
			value = tag_data.GetTagProperty("enabled").AsBool();
		}
		this.choise_random_data_.Add(new KeyValuePair<string, KeyValuePair<string, bool>>(key, new KeyValuePair<string, bool>(tag_data.GetTagProperty("label").AsString(), value)));
		return false;
	}

	public bool TagChoicesRandomSelect(KagTagSupport tag_data)
	{
		base.CheckAbsolutelyNecessaryTag(tag_data, "choicesrandomselect", new string[]
		{
			"num"
		});
		int num = tag_data.GetTagProperty("num").AsInteger();
		if (this.choise_random_data_.Count < num)
		{
			num = this.choise_random_data_.Count;
		}
		System.Random random = new System.Random();
		int i = this.choise_random_data_.Count;
		while (i > 1)
		{
			i--;
			int index = random.Next(i + 1);
			KeyValuePair<string, KeyValuePair<string, bool>> value = this.choise_random_data_[index];
			this.choise_random_data_[index] = this.choise_random_data_[i];
			this.choise_random_data_[i] = value;
		}
		for (int j = 0; j < num; j++)
		{
			this.choise_data_.Add(this.choise_random_data_[j]);
		}
		this.choise_random_data_.Clear();
		return false;
	}

	public bool TagCallDialog(KagTagSupport tag_data)
	{
		base.CheckAbsolutelyNecessaryTag(tag_data, "calldialog", new string[]
		{
			"text",
			"label"
		});
		string label = tag_data.GetTagProperty("label").AsString();
		GameMain.Instance.SysDlg.Show(tag_data.GetTagProperty("text").AsString(), SystemDialog.TYPE.OK, delegate
		{
			GameMain.Instance.SysDlg.Close();
			this.kag_.GoToLabel(label);
			this.kag_.Exec();
		}, null);
		return false;
	}

	public bool Tag_ChoicesShow(KagTagSupport tag_data)
	{
		Action<string, string> onClickCallBack = delegate(string choices_title, string label_name)
		{
			this.kag_.GoToLabel(label_name);
			this.kag_.Exec();
		};
		this.message_mgr_.CreateSelectButtons(this.choise_data_, onClickCallBack);
		this.choise_data_.Clear();
		return false;
	}

	protected override void PlayMaidMotion(Maid maid, string fn, bool additive = false, bool loop = false, bool boAddQue = false, float val = 0.5f)
	{
		if (maid == null || maid.body0 == null || !maid.body0.isLoadedBody)
		{
			this.script_mgr_.StopMotionScript();
			return;
		}
		if (!boAddQue)
		{
			WaitEventList waitEventList = base.GetWaitEventList("motion_script");
			waitEventList.Clear();
		}
		this.script_mgr_.StopMotionScript();
		base.PlayMaidMotion(maid, fn, additive, loop, boAddQue, val);
	}

	public void SetSettingSkipMode(bool is_skip)
	{
		this.skip_mode_ = is_skip;
	}

	public bool GetSettingSkipMode()
	{
		return this.skip_mode_;
	}

	public bool skip_mode
	{
		get
		{
			return this.skip_mode_ || GameMain.Instance.IsForceSkip();
		}
	}

	public bool auto_mode { get; set; }

	public bool vr_commu_mode { get; set; }

	public int auto_wait_count
	{
		get
		{
			return (100 - GameMain.Instance.CMSystem.MsgAutoSpeed) * 30;
		}
	}

	public Dictionary<string, string> tag_backup
	{
		get
		{
			return this.tag_backup_;
		}
	}

	public override string GetKagClassName()
	{
		return "ADV kag";
	}

	public MessageWindowMgr MessageWindowMgr
	{
		get
		{
			return this.message_mgr_;
		}
	}

	private bool skip_mode_;

	private MessageWindowMgr message_mgr_;

	private ADVKagManager.TextData text_data_;

	private List<KeyValuePair<string, KeyValuePair<string, bool>>> choise_data_ = new List<KeyValuePair<string, KeyValuePair<string, bool>>>();

	private List<KeyValuePair<string, KeyValuePair<string, bool>>> choise_random_data_ = new List<KeyValuePair<string, KeyValuePair<string, bool>>>();

	private ulong update_count_;

	private Dictionary<string, string> tag_backup_ = new Dictionary<string, string>();

	protected struct TextData
	{
		public string talk_name;

		public string text;

		public string voice_file;

		public string vice_name;

		public Maid voice_maid;
	}
}
