using System;
using System.Collections;
using System.IO;
using DarkTonic.MasterAudio;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x020000ED RID: 237
public class DrawViewController : MonoBehaviour, IBeginDragHandler, IEventSystemHandler, IDragHandler, IEndDragHandler
{
	// Token: 0x06001531 RID: 5425 RVA: 0x00197C80 File Offset: 0x00195E80
	private void Awake()
	{
		this.rectTransform = base.GetComponent<RectTransform>();
		this.drawImage = base.GetComponent<Image>();
		this.drawSettings = new DrawSettings();
		this.drawSettings.lineWidth = this.lineWidth;
		this.resetColor = new Color(0f, 0f, 0f, 0f);
		this.Initialize();
		this.ResetTexture();
	}

	// Token: 0x06001532 RID: 5426 RVA: 0x00197CEC File Offset: 0x00195EEC
	public void CheckAgainstMask()
	{
		int num = 0;
		int num2 = this.currentColors.Length;
		int num3 = 0;
		for (int i = 0; i < num2; i++)
		{
			if ((double)this.currentColors[i].a < 0.5 && (double)this.maskColors[i].r > 0.5)
			{
				num++;
			}
			if ((double)this.maskColors[i].r > 0.5)
			{
				num3++;
			}
		}
		int num4 = num3 - num;
		this.percentComplete = (float)num4 / (float)num3;
		this.meter.fillAmount = this.percentComplete;
		float num5 = this.percentComplete;
		this.meter.color = Color.Lerp(this.notEnoughColor, this.completeColor, num5);
		this.uncoveredText.text = Mathf.Ceil(this.percentComplete * 100f).ToString() + "%";
	}

	// Token: 0x06001533 RID: 5427 RVA: 0x00197DED File Offset: 0x00195FED
	public void SaveAsSprite(string filename, bool saveOver, string scrollName, Library.Inventory invRow, string lookFor)
	{
		base.StartCoroutine(this.Save(filename, saveOver, scrollName, invRow, lookFor));
	}

	// Token: 0x06001534 RID: 5428 RVA: 0x00197E03 File Offset: 0x00196003
	private IEnumerator Save(string filename, bool saveOver, string scrollName, Library.Inventory invRow, string lookFor)
	{
		yield return new WaitForEndOfFrame();
		Vector3[] array = new Vector3[4];
		this.drawImage.gameObject.GetComponent<RectTransform>().GetWorldCorners(array);
		Vector2 vector = RectTransformUtility.WorldToScreenPoint(Links.x.menuCamera, array[0]);
		Vector2 vector2 = RectTransformUtility.WorldToScreenPoint(Links.x.menuCamera, array[1]);
		ref Vector2 ptr = RectTransformUtility.WorldToScreenPoint(Links.x.menuCamera, array[2]);
		int num = Mathf.RoundToInt(vector2.y - vector.y);
		int num2 = Mathf.RoundToInt(ptr.x - vector.x);
		Texture2D texture2D = new Texture2D(num2, num, TextureFormat.RGB24, false);
		Rect rect = new Rect(Mathf.Round(vector.x), Mathf.Round(vector.y), (float)num2, (float)num);
		texture2D.ReadPixels(rect, 0, 0);
		texture2D.Apply();
		if (num2 > 512)
		{
			Texture2D texture2D2 = this.ScaleTexture(texture2D, 512, 512);
			byte[] array2 = texture2D2.EncodeToPNG();
			Object.Destroy(texture2D);
			Object.Destroy(texture2D2);
			File.WriteAllBytes(filename, array2);
		}
		else
		{
			byte[] array3 = texture2D.EncodeToPNG();
			Object.Destroy(texture2D);
			File.WriteAllBytes(filename, array3);
		}
		Links.x.hudControl.chalkRubbing.GetComponent<Animator>().Play("ChalkRubbingClose");
		Links.x.dialogue.inventoryItemImage.sprite = Links.x.archives.GetItem(invRow._UIModel);
		Debug.Log(filename + " " + lookFor);
		Links.x.archives.OverrideRune(lookFor, filename, "", lookFor);
		MasterAudio.PlaySoundAndForget("Inventory", 0.6f, new float?(1f), 0f, invRow._Sound, null);
		Links.x.dialogue.dialoguePopupBox.SetActive(true);
		this.str.Clear();
		if (!saveOver)
		{
			this.str.Append("Item gained\n<size=1.6em><font=Bold>");
		}
		else
		{
			this.str.Append("Item updated\n<size=1.6em><font=Bold>");
		}
		this.str.Append(invRow._DisplayName);
		this.str.Append("</font></size>");
		Links.x.dialogue.itemPopupText.text = this.str.ToString();
		Links.x.dialogue.dialoguePopupBoxAnimator.Play("DialoguePopsItemOn", -1, 0f);
		yield return new WaitForSeconds(4.15f);
		MasterAudio.PlaySoundAndForget("Feedback", 0.6f, new float?(1f), 0f, "Box Close", null);
		yield return new WaitForSeconds(0.6f);
		Links.x.dialogue.dialoguePopupBox.SetActive(false);
		Links.x.dialogue.itemPopupText.text = "";
		Links.x.hudControl.FinishedSavingChalkRubbing(Mathf.Ceil(this.percentComplete * 100f), scrollName, invRow);
		this.ResetTexture();
		yield break;
	}

	// Token: 0x06001535 RID: 5429 RVA: 0x00197E38 File Offset: 0x00196038
	private Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
	{
		Texture2D texture2D = new Texture2D(targetWidth, targetHeight, source.format, true);
		Color[] pixels = texture2D.GetPixels(0);
		float num = 1f / (float)source.width * ((float)source.width / (float)targetWidth);
		float num2 = 1f / (float)source.height * ((float)source.height / (float)targetHeight);
		for (int i = 0; i < pixels.Length; i++)
		{
			pixels[i] = source.GetPixelBilinear(num * ((float)i % (float)targetWidth), num2 * Mathf.Floor((float)(i / targetWidth)));
		}
		texture2D.SetPixels(pixels, 0);
		texture2D.Apply();
		return texture2D;
	}

	// Token: 0x06001536 RID: 5430 RVA: 0x00197ED4 File Offset: 0x001960D4
	public void Initialize()
	{
		if (!this.drawImage)
		{
			this.drawImage = base.gameObject.GetComponent<Image>();
			this.rectTransform = base.gameObject.GetComponent<RectTransform>();
			this.drawSettings = new DrawSettings();
		}
		this.drawSprite = this.drawImage.sprite;
		this.drawTexture = this.drawSprite.texture;
		if (this.mask)
		{
			this.maskColors = this.mask.GetPixels32();
		}
		this.resetColorsArray = new Color[(int)this.drawSprite.rect.width * (int)this.drawSprite.rect.height];
		for (int i = 0; i < this.resetColorsArray.Length; i++)
		{
			this.resetColorsArray[i] = this.resetColor;
		}
	}

	// Token: 0x06001537 RID: 5431 RVA: 0x00197FB4 File Offset: 0x001961B4
	private void KeyboardInput()
	{
		if (!Input.GetKey(KeyCode.LeftControl))
		{
			Input.GetKey(KeyCode.RightControl);
		}
		Input.GetKeyDown(KeyCode.Z);
	}

	// Token: 0x06001538 RID: 5432 RVA: 0x00197FD8 File Offset: 0x001961D8
	public void Paint(Vector2 pixelPosition)
	{
		this.currentColors = this.drawTexture.GetPixels32();
		if (this.previousDragPosition == Vector2.zero)
		{
			this.MarkPixelsToColour(pixelPosition);
		}
		else
		{
			this.ColorBetween(this.previousDragPosition, pixelPosition);
		}
		this.ApplyCurrentColors();
		this.previousDragPosition = pixelPosition;
	}

	// Token: 0x06001539 RID: 5433 RVA: 0x0019802C File Offset: 0x0019622C
	public void MarkPixelsToColour(Vector2 centerPixel)
	{
		int num = (int)centerPixel.x;
		int num2 = (int)centerPixel.y;
		for (int i = num - this.drawSettings.lineWidth; i <= num + this.drawSettings.lineWidth; i++)
		{
			if (i < (int)this.drawSprite.rect.width && i >= 0)
			{
				for (int j = num2 - this.drawSettings.lineWidth; j <= num2 + this.drawSettings.lineWidth; j++)
				{
					this.MarkPixelToChange(i, j);
				}
			}
		}
	}

	// Token: 0x0600153A RID: 5434 RVA: 0x001980BC File Offset: 0x001962BC
	public void ColorBetween(Vector2 startPoint, Vector2 endPoint)
	{
		float num = Vector2.Distance(startPoint, endPoint);
		float num2 = 1f / num;
		for (float num3 = 0f; num3 <= 1f; num3 += num2)
		{
			Vector2 vector = Vector2.Lerp(startPoint, endPoint, num3);
			this.MarkPixelsToColour(vector);
		}
	}

	// Token: 0x0600153B RID: 5435 RVA: 0x00198100 File Offset: 0x00196300
	public void MarkPixelToChange(int x, int y)
	{
		int num = y * (int)this.drawSprite.rect.width + x;
		if (num > this.currentColors.Length || num < 0)
		{
			return;
		}
		if (num > -1 && num < this.currentColors.Length)
		{
			this.currentColors[num] = this.drawSettings.drawColor;
		}
	}

	// Token: 0x0600153C RID: 5436 RVA: 0x00198161 File Offset: 0x00196361
	public void ApplyCurrentColors()
	{
		this.drawTexture.SetPixels32(this.currentColors);
		this.drawTexture.Apply();
		if (this.checkMask)
		{
			this.CheckAgainstMask();
		}
	}

	// Token: 0x0600153D RID: 5437 RVA: 0x00198190 File Offset: 0x00196390
	public void ResetTexture()
	{
		if (this.drawTexture)
		{
			this.drawTexture.SetPixels(this.resetColorsArray);
			this.drawTexture.Apply();
			this.currentColors = this.drawTexture.GetPixels32();
			if (this.checkMask)
			{
				this.CheckAgainstMask();
			}
		}
	}

	// Token: 0x0600153E RID: 5438 RVA: 0x001981E5 File Offset: 0x001963E5
	public void OnBeginDrag(PointerEventData eventData)
	{
		this.drawSettings.AddUndo(this.drawTexture.GetPixels32());
		this.OnDrag(eventData);
	}

	// Token: 0x0600153F RID: 5439 RVA: 0x00198204 File Offset: 0x00196404
	public void OnDrag(PointerEventData eventData)
	{
		Vector2 zero = Vector2.zero;
		if (eventData != null)
		{
			if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(this.rectTransform, eventData.position, eventData.pressEventCamera, out zero))
			{
				return;
			}
		}
		else
		{
			zero = this.joystickPosition;
		}
		if (zero.x < this.rectTransform.rect.width && zero.y < this.rectTransform.rect.height && zero.x > 0f && zero.y > 0f)
		{
			float num = this.drawImage.sprite.rect.width / this.rectTransform.rect.width;
			zero = new Vector2(zero.x * num, zero.y * num);
			this.Paint(zero);
			this.previousDragPosition = zero;
			return;
		}
		this.previousDragPosition = Vector2.zero;
	}

	// Token: 0x06001540 RID: 5440 RVA: 0x001982EE File Offset: 0x001964EE
	public void OnEndDrag(PointerEventData eventData)
	{
		this.previousDragPosition = Vector2.zero;
	}

	// Token: 0x06001541 RID: 5441 RVA: 0x001982FB File Offset: 0x001964FB
	public void SetDrawColor(Color color)
	{
		this.drawSettings.SetDrawColour(color);
	}

	// Token: 0x06001542 RID: 5442 RVA: 0x00198309 File Offset: 0x00196509
	public Color GetDrawColor()
	{
		return this.drawSettings.drawColor;
	}

	// Token: 0x06001543 RID: 5443 RVA: 0x00198316 File Offset: 0x00196516
	public void SetDrawLineWidth(int width)
	{
		this.drawSettings.SetLineWidth(width);
	}

	// Token: 0x06001544 RID: 5444 RVA: 0x00198324 File Offset: 0x00196524
	public int GetDrawLineWidth()
	{
		return this.drawSettings.lineWidth;
	}

	// Token: 0x06001545 RID: 5445 RVA: 0x00198331 File Offset: 0x00196531
	public void SetDrawTransparency(float transparency)
	{
		this.drawSettings.SetAlpha(transparency);
	}

	// Token: 0x06001546 RID: 5446 RVA: 0x0019833F File Offset: 0x0019653F
	public float GetDrawTransparency()
	{
		return this.drawSettings.transparency;
	}

	// Token: 0x04002511 RID: 9489
	private DrawSettings drawSettings;

	// Token: 0x04002512 RID: 9490
	private Image drawImage;

	// Token: 0x04002513 RID: 9491
	private Sprite drawSprite;

	// Token: 0x04002514 RID: 9492
	private Texture2D drawTexture;

	// Token: 0x04002515 RID: 9493
	private Vector2 previousDragPosition;

	// Token: 0x04002516 RID: 9494
	private Color[] resetColorsArray;

	// Token: 0x04002517 RID: 9495
	private Color resetColor;

	// Token: 0x04002518 RID: 9496
	private Color32[] currentColors;

	// Token: 0x04002519 RID: 9497
	private Color32[] maskColors;

	// Token: 0x0400251A RID: 9498
	private RectTransform rectTransform;

	// Token: 0x0400251B RID: 9499
	[Header("Settings")]
	public int lineWidth = 5;

	// Token: 0x0400251C RID: 9500
	[Header("For Chalk Rubbing")]
	public bool checkMask;

	// Token: 0x0400251D RID: 9501
	public Texture2D mask;

	// Token: 0x0400251E RID: 9502
	public float percentComplete;

	// Token: 0x0400251F RID: 9503
	public Image meter;

	// Token: 0x04002520 RID: 9504
	public Color notEnoughColor;

	// Token: 0x04002521 RID: 9505
	public Color closeColor;

	// Token: 0x04002522 RID: 9506
	public Color completeColor;

	// Token: 0x04002523 RID: 9507
	public TextMeshProUGUI uncoveredText;

	// Token: 0x04002524 RID: 9508
	private StringFast str = new StringFast(64);

	// Token: 0x04002525 RID: 9509
	public Vector2 joystickPosition;
}
