using System;
using System.Collections.Generic;
using I2.Loc;
using MaidStatus;
using UnityEngine;
using wf;

public class ColorPaletteManager : EmptyWindow
{
	private EyePartsTab selectEyeType
	{
		get
		{
			return this.maid.status.eyePartsTab;
		}
		set
		{
			this.maid.status.eyePartsTab = value;
		}
	}

	public override void Awake()
	{
		base.Awake();
		this.init_pos_ = this.resetWindowPos;
		this.uiManager.onChangeValue = new Action<ColorPaletteUIManager>(this.OnChangeUIValue);
		this.categoryTabPanel.Start();
		UIWFTabButton[] componentsInChildren = this.categoryTabPanel.GetComponentsInChildren<UIWFTabButton>(false);
		foreach (UIWFTabButton uiwftabButton in componentsInChildren)
		{
			EventDelegate.Add(uiwftabButton.onSelect, new EventDelegate.Callback(this.OnSelectCategory));
		}
		this.eyeTypeTabPanel.Start();
		componentsInChildren = this.eyeTypeTabPanel.GetComponentsInChildren<UIWFTabButton>(false);
		foreach (UIWFTabButton uiwftabButton2 in componentsInChildren)
		{
			EventDelegate.Add(uiwftabButton2.onSelect, new EventDelegate.Callback(this.OnSelectEyeType));
		}
		if (this.colorPresetGrid != null)
		{
			for (int k = 0; k < this.colorPresetNum; k++)
			{
				GameObject gameObject = Utility.CreatePrefab(this.colorPresetGrid.gameObject, "SceneEdit\\ColorPalette\\Prefabs\\ColorPresetItem", true);
				ColorPresetItem component = gameObject.GetComponent<ColorPresetItem>();
				component.slotNo = k;
				component.onSaveColorPresetEvent = new Func<ColorPaletteManager.ColorData>(this.OnSaveColorPreset);
				component.onLoadColorPresetEvent = new Action<Dictionary<string, int>>(this.OnLoadColorPreset);
			}
			Utility.ResetNGUI(this.colorPresetGrid);
		}
		this.UpdateChildren();
		this.baseDraw = false;
	}

	public void Call(int maidNo, MaidParts.PARTS_COLOR colorType)
	{
		base.gameObject.SetActive(true);
		this.visible = true;
		this.maid = GameMain.Instance.CharacterMgr.GetMaid(maidNo);
		this.category = ColorPaletteManager.Category.Main;
		this.colorData = ColorPaletteManager.ColorData.Create(this.maid, colorType);
		UIWFTabButton[] componentsInChildren = this.categoryTabPanel.GetComponentsInChildren<UIWFTabButton>(true);
		componentsInChildren[2].gameObject.SetActive(this.colorData.enabledOutLine);
		Localize component = componentsInChildren[2].GetComponent<Localize>();
		if (this.colorData.enabledOutLine)
		{
			component = componentsInChildren[0].GetComponent<Localize>();
			component.SetTerm("SceneEdit/カラーパレット/スプライト/タブ_基本色");
			component = componentsInChildren[1].GetComponent<Localize>();
			component.SetTerm("SceneEdit/カラーパレット/スプライト/タブ_影色");
			componentsInChildren[0].pixelSnap = true;
			componentsInChildren[1].pixelSnap = true;
		}
		else
		{
			component = componentsInChildren[0].GetComponent<Localize>();
			component.SetTerm("SceneEdit/カラーパレット/スプライト/タブ_基本色_ロング");
			component = componentsInChildren[1].GetComponent<Localize>();
			component.SetTerm("SceneEdit/カラーパレット/スプライト/タブ_影色_ロング");
			componentsInChildren[0].pixelSnap = true;
			componentsInChildren[1].pixelSnap = true;
			componentsInChildren[0].GetComponent<UISprite>().MakePixelPerfect();
			componentsInChildren[1].GetComponent<UISprite>().MakePixelPerfect();
		}
		Vector3 localPosition = componentsInChildren[1].transform.localPosition;
		Vector3 localPosition2 = new Vector3(componentsInChildren[0].transform.localPosition.x + (float)componentsInChildren[0].GetComponent<UIWidget>().width, localPosition.y, localPosition.z);
		componentsInChildren[1].transform.localPosition = localPosition2;
		componentsInChildren = this.eyeTypeTabPanel.GetComponentsInChildren<UIWFTabButton>(true);
		foreach (UIWFTabButton uiwftabButton in componentsInChildren)
		{
			uiwftabButton.gameObject.SetActive(colorType == MaidParts.PARTS_COLOR.EYE_L || colorType == MaidParts.PARTS_COLOR.EYE_R);
		}
		if ((colorType == MaidParts.PARTS_COLOR.EYE_L || colorType == MaidParts.PARTS_COLOR.EYE_R) && this.selectEyeType == EyePartsTab.LR)
		{
			ColorPaletteManager.ColorData obj;
			if (colorType == MaidParts.PARTS_COLOR.EYE_L)
			{
				obj = ColorPaletteManager.ColorData.Create(this.maid, MaidParts.PARTS_COLOR.EYE_R);
			}
			else
			{
				obj = ColorPaletteManager.ColorData.Create(this.maid, MaidParts.PARTS_COLOR.EYE_L);
			}
			if (!this.colorData.EqualsData(obj))
			{
				this.maid.status.eyePartsTab = EyePartsTab.R;
			}
		}
		if (colorType == MaidParts.PARTS_COLOR.EYE_L || colorType == MaidParts.PARTS_COLOR.EYE_R)
		{
			this.SetEyeSelect(this.selectEyeType);
		}
		else
		{
			this.SetCategory(this.category);
		}
	}

	public void Close()
	{
		this.visible = false;
		base.gameObject.SetActive(false);
	}

	public void ResetUIPos()
	{
		this.ResetPosition();
	}

	public void SetCategory(ColorPaletteManager.Category category)
	{
		UIWFTabButton[] componentsInChildren = this.categoryTabPanel.GetComponentsInChildren<UIWFTabButton>(false);
		if (category == ColorPaletteManager.Category.OutLine && componentsInChildren.Length < 3)
		{
			category = (this.category = ColorPaletteManager.Category.Main);
		}
		this.categoryTabPanel.Select(componentsInChildren[(int)category]);
	}

	public void SetEyeSelect(EyePartsTab eyeType)
	{
		UIWFTabButton[] componentsInChildren = this.eyeTypeTabPanel.GetComponentsInChildren<UIWFTabButton>(false);
		this.eyeTypeTabPanel.Select(componentsInChildren[this.PartsTypeToIndex(eyeType)]);
	}

	private void UpdateUI()
	{
		ColorPaletteManager.ColorData.CommonItem commonItem;
		if (this.category == ColorPaletteManager.Category.Main)
		{
			commonItem = this.colorData.main;
		}
		else if (this.category == ColorPaletteManager.Category.Shadow)
		{
			commonItem = this.colorData.shadow;
		}
		else
		{
			commonItem = this.colorData.outline;
		}
		this.uiManager.visibleShadowRate = (this.category == ColorPaletteManager.Category.Main || this.category == ColorPaletteManager.Category.Shadow);
		int shadowRate = (!this.uiManager.visibleShadowRate) ? 0 : this.colorData.shadowRate;
		this.uiManager.hue = commonItem.hue;
		this.uiManager.chroma = commonItem.chroma;
		this.uiManager.brightness = commonItem.brightness;
		this.uiManager.contrast = commonItem.contrast;
		this.uiManager.shadowRate = shadowRate;
	}

	private void OnChangeUIValue(ColorPaletteUIManager mgr)
	{
		if (this.maid == null)
		{
			return;
		}
		if (this.category == ColorPaletteManager.Category.Main)
		{
			this.colorData.main.hue = this.uiManager.hue;
			this.colorData.main.chroma = this.uiManager.chroma;
			this.colorData.main.brightness = this.uiManager.brightness;
			this.colorData.main.contrast = this.uiManager.contrast;
			this.colorData.shadowRate = this.uiManager.shadowRate;
		}
		else if (this.category == ColorPaletteManager.Category.Shadow)
		{
			this.colorData.shadow.hue = this.uiManager.hue;
			this.colorData.shadow.chroma = this.uiManager.chroma;
			this.colorData.shadow.brightness = this.uiManager.brightness;
			this.colorData.shadow.contrast = this.uiManager.contrast;
			this.colorData.shadowRate = this.uiManager.shadowRate;
		}
		else if (this.category == ColorPaletteManager.Category.OutLine)
		{
			this.colorData.outline.hue = this.uiManager.hue;
			this.colorData.outline.chroma = this.uiManager.chroma;
			this.colorData.outline.brightness = this.uiManager.brightness;
			this.colorData.outline.contrast = this.uiManager.contrast;
		}
		ColorPaletteManager.ColorData.Apply(this.maid, this.colorData);
		if ((this.colorData.colorType == MaidParts.PARTS_COLOR.EYE_L || this.colorData.colorType == MaidParts.PARTS_COLOR.EYE_R) && this.selectEyeType == EyePartsTab.LR)
		{
			ColorPaletteManager.ColorData data = ColorPaletteManager.ColorData.Create(this.maid, MaidParts.PARTS_COLOR.EYE_L);
			data.colorType = MaidParts.PARTS_COLOR.EYE_R;
			ColorPaletteManager.ColorData.Apply(this.maid, data);
		}
	}

	private void OnSelectCategory()
	{
		if (!UIWFSelectButton.selected)
		{
			return;
		}
		UIWFTabButton[] componentsInChildren = this.categoryTabPanel.GetComponentsInChildren<UIWFTabButton>(true);
		ColorPaletteManager.Category category = ColorPaletteManager.Category.Main;
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			if (componentsInChildren[i] == UIWFSelectButton.current)
			{
				category = (ColorPaletteManager.Category)i;
				break;
			}
		}
		this.category = category;
		this.UpdateUI();
	}

	private void OnSelectEyeType()
	{
		if (!UIWFSelectButton.selected)
		{
			return;
		}
		UIWFTabButton[] componentsInChildren = this.eyeTypeTabPanel.GetComponentsInChildren<UIWFTabButton>(true);
		int index = 0;
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			if (componentsInChildren[i] == UIWFSelectButton.current)
			{
				index = i;
				break;
			}
		}
		this.selectEyeType = this.IndexToEyePartsType(index);
		if (this.selectEyeType == EyePartsTab.LR)
		{
			ColorPaletteManager.ColorData data = ColorPaletteManager.ColorData.Create(this.maid, MaidParts.PARTS_COLOR.EYE_L);
			data.colorType = MaidParts.PARTS_COLOR.EYE_R;
			ColorPaletteManager.ColorData.Apply(this.maid, data);
		}
		MaidParts.PARTS_COLOR colorType = MaidParts.PARTS_COLOR.EYE_L;
		if (this.selectEyeType == EyePartsTab.R)
		{
			colorType = MaidParts.PARTS_COLOR.EYE_R;
		}
		this.colorData = ColorPaletteManager.ColorData.Create(this.maid, colorType);
		this.SetCategory(this.category);
	}

	private EyePartsTab IndexToEyePartsType(int index)
	{
		return (index != 0) ? ((index != 1) ? EyePartsTab.LR : EyePartsTab.R) : EyePartsTab.L;
	}

	private int PartsTypeToIndex(EyePartsTab tab)
	{
		return (tab != EyePartsTab.L) ? ((tab != EyePartsTab.R) ? 2 : 1) : 0;
	}

	private ColorPaletteManager.ColorData OnSaveColorPreset()
	{
		return this.colorData;
	}

	private void OnLoadColorPreset(Dictionary<string, int> data)
	{
		this.colorData = ColorPaletteManager.ColorData.RestoreData(this.colorData.colorType, data);
		ColorPaletteManager.ColorData.Apply(this.maid, this.colorData);
		if (this.colorData.colorType == MaidParts.PARTS_COLOR.EYE_L || this.colorData.colorType == MaidParts.PARTS_COLOR.EYE_R)
		{
			this.SetEyeSelect(this.selectEyeType);
		}
		else
		{
			this.SetCategory(this.category);
		}
	}

	[SerializeField]
	private ColorPaletteUIManager uiManager;

	[SerializeField]
	private UIWFTabPanel categoryTabPanel;

	[SerializeField]
	private UIWFTabPanel eyeTypeTabPanel;

	[SerializeField]
	private UIGrid colorPresetGrid;

	[SerializeField]
	private int colorPresetNum;

	[SerializeField]
	private Vector3 resetWindowPos;

	private ColorPaletteManager.Category category;

	private Maid maid;

	private ColorPaletteManager.ColorData colorData;

	public enum Category
	{
		Main,
		Shadow,
		OutLine
	}

	public struct ColorData
	{
		public bool EqualsData(ColorPaletteManager.ColorData obj)
		{
			return this.enabledOutLine == obj.enabledOutLine && this.shadowRate == obj.shadowRate && this.shadowRate == obj.shadowRate && this.main.Equals(obj.main) && this.shadow.Equals(obj.shadow) && this.outline.Equals(obj.outline);
		}

		public Dictionary<string, int> GetStoreData()
		{
			Dictionary<string, int> result = new Dictionary<string, int>();
			Action<string, ColorPaletteManager.ColorData.CommonItem> action = delegate(string connectName, ColorPaletteManager.ColorData.CommonItem writeData)
			{
				result.Add(connectName + "_hue", writeData.hue);
				result.Add(connectName + "_chroma", writeData.chroma);
				result.Add(connectName + "_brightness", writeData.brightness);
				result.Add(connectName + "_contrast", writeData.contrast);
			};
			action("main", this.main);
			action("shadow", this.shadow);
			result.Add("shadowRate", this.shadowRate);
			result.Add("existOutLineData", (!this.enabledOutLine) ? 0 : 1);
			if (this.enabledOutLine)
			{
				action("outline", this.outline);
			}
			return result;
		}

		public static ColorPaletteManager.ColorData RestoreData(MaidParts.PARTS_COLOR colorType, Dictionary<string, int> storeData)
		{
			ColorPaletteManager.ColorData result = default(ColorPaletteManager.ColorData);
			result.colorType = colorType;
			result.enabledOutLine = (colorType == MaidParts.PARTS_COLOR.HAIR || colorType == MaidParts.PARTS_COLOR.SKIN);
			Func<string, ColorPaletteManager.ColorData.CommonItem> func = (string connectName) => new ColorPaletteManager.ColorData.CommonItem
			{
				hue = storeData[connectName + "_hue"],
				chroma = storeData[connectName + "_chroma"],
				brightness = storeData[connectName + "_brightness"],
				contrast = storeData[connectName + "_contrast"]
			};
			result.main = func("main");
			result.shadow = func("shadow");
			result.shadowRate = storeData["shadowRate"];
			if (result.enabledOutLine && storeData["existOutLineData"] == 1)
			{
				result.outline = func("outline");
			}
			return result;
		}

		public static ColorPaletteManager.ColorData Create(Maid maid, MaidParts.PARTS_COLOR colorType)
		{
			ColorPaletteManager.ColorData result = default(ColorPaletteManager.ColorData);
			if (colorType == MaidParts.PARTS_COLOR.HAIR || colorType == MaidParts.PARTS_COLOR.HAIR_OUTLINE)
			{
				colorType = MaidParts.PARTS_COLOR.HAIR;
			}
			else if (colorType == MaidParts.PARTS_COLOR.SKIN || colorType == MaidParts.PARTS_COLOR.SKIN_OUTLINE)
			{
				colorType = MaidParts.PARTS_COLOR.SKIN;
			}
			result.colorType = colorType;
			result.enabledOutLine = (colorType == MaidParts.PARTS_COLOR.HAIR || colorType == MaidParts.PARTS_COLOR.HAIR_OUTLINE || colorType == MaidParts.PARTS_COLOR.SKIN || colorType == MaidParts.PARTS_COLOR.SKIN_OUTLINE);
			MaidParts.PartsColor partsColor = maid.Parts.GetPartsColor(colorType);
			result.main.hue = partsColor.m_nMainHue;
			result.main.chroma = partsColor.m_nMainChroma;
			result.main.brightness = partsColor.m_nMainBrightness;
			result.main.contrast = partsColor.m_nMainContrast;
			result.shadow.hue = partsColor.m_nShadowHue;
			result.shadow.chroma = partsColor.m_nShadowChroma;
			result.shadow.brightness = partsColor.m_nShadowBrightness;
			result.shadow.contrast = partsColor.m_nShadowContrast;
			result.shadowRate = partsColor.m_nShadowRate;
			if (result.enabledOutLine)
			{
				MaidParts.PartsColor partsColor2 = (colorType != MaidParts.PARTS_COLOR.HAIR && colorType != MaidParts.PARTS_COLOR.HAIR_OUTLINE) ? maid.Parts.GetPartsColor(MaidParts.PARTS_COLOR.SKIN_OUTLINE) : maid.Parts.GetPartsColor(MaidParts.PARTS_COLOR.HAIR_OUTLINE);
				result.outline.hue = partsColor2.m_nMainHue;
				result.outline.chroma = partsColor2.m_nMainChroma;
				result.outline.brightness = partsColor2.m_nMainBrightness;
				result.outline.contrast = partsColor2.m_nMainContrast;
			}
			return result;
		}

		public static void Apply(Maid maid, ColorPaletteManager.ColorData data)
		{
			MaidParts.PartsColor f_cColor = default(MaidParts.PartsColor);
			f_cColor.m_nMainHue = data.main.hue;
			f_cColor.m_nMainChroma = data.main.chroma;
			f_cColor.m_nMainBrightness = data.main.brightness;
			f_cColor.m_nMainContrast = data.main.contrast;
			f_cColor.m_nShadowHue = data.shadow.hue;
			f_cColor.m_nShadowChroma = data.shadow.chroma;
			f_cColor.m_nShadowBrightness = data.shadow.brightness;
			f_cColor.m_nShadowContrast = data.shadow.contrast;
			f_cColor.m_nShadowRate = data.shadowRate;
			if (data.enabledOutLine)
			{
				MaidParts.PartsColor f_cColor2 = default(MaidParts.PartsColor);
				f_cColor2.m_nMainHue = data.outline.hue;
				f_cColor2.m_nMainChroma = data.outline.chroma;
				f_cColor2.m_nMainBrightness = data.outline.brightness;
				f_cColor2.m_nMainContrast = data.outline.contrast;
				if (data.colorType == MaidParts.PARTS_COLOR.HAIR)
				{
					maid.Parts.SetPartsColor(MaidParts.PARTS_COLOR.HAIR_OUTLINE, f_cColor2);
				}
				else
				{
					maid.Parts.SetPartsColor(MaidParts.PARTS_COLOR.SKIN_OUTLINE, f_cColor2);
				}
			}
			maid.Parts.SetPartsColor(data.colorType, f_cColor);
		}

		public MaidParts.PARTS_COLOR colorType;

		public bool enabledOutLine;

		public int shadowRate;

		public ColorPaletteManager.ColorData.CommonItem main;

		public ColorPaletteManager.ColorData.CommonItem shadow;

		public ColorPaletteManager.ColorData.CommonItem outline;

		public struct CommonItem
		{
			public bool Equals(ColorPaletteManager.ColorData.CommonItem obj)
			{
				return this.hue == obj.hue && this.chroma == obj.chroma && this.brightness == obj.brightness && this.contrast == obj.contrast;
			}

			public int hue;

			public int chroma;

			public int brightness;

			public int contrast;
		}
	}
}
