using System;
using System.IO;
using UnityEngine;

public class PhotoCustomObjectPlane : BasePhotoCustomObject
{
	public override byte[] imageBinarys
	{
		get
		{
			return this.imageBinary;
		}
	}

	public override BasePhotoCustomObject.Type type
	{
		get
		{
			return BasePhotoCustomObject.Type.Plane;
		}
	}

	public override Texture2D mainTexture
	{
		get
		{
			return this.texture;
		}
	}

	public bool isAlphaTexture
	{
		get
		{
			return this.isAlphaTexture_;
		}
	}

	public PhotoCustomObjectPlane.BackRenderingType backRenderingType
	{
		get
		{
			return this.backRenderingType_;
		}
		set
		{
			this.backRenderingType_ = value;
			if (this.material == null)
			{
				return;
			}
			if (this.backRenderingType_ == PhotoCustomObjectPlane.BackRenderingType.Mirror)
			{
				this.material.SetFloat("_Cull", 0f);
				this.subTextureObject.SetActive(false);
			}
			else if (this.backRenderingType_ == PhotoCustomObjectPlane.BackRenderingType.Flip)
			{
				this.material.SetFloat("_Cull", 2f);
				this.subTextureObject.SetActive(true);
			}
			else if (this.backRenderingType_ == PhotoCustomObjectPlane.BackRenderingType.None)
			{
				this.material.SetFloat("_Cull", 2f);
				this.subTextureObject.SetActive(false);
			}
		}
	}

	public override void Awake()
	{
		base.Awake();
		this.material = new Material(Shader.Find("CM3D2/Unlit_Texture_Photo_MyObject"));
		foreach (Renderer renderer in this.texRenderers)
		{
			if (renderer != null)
			{
				renderer.material = this.material;
			}
		}
		this.subTextureObject.SetActive(false);
		this.scaleTransform.gameObject.SetActive(false);
	}

	public override void OnDestroy()
	{
		base.OnDestroy();
		this.ReleaseTexture();
	}

	public override void Clear()
	{
		base.Clear();
		if (this.material != null && this.material.mainTexture != null)
		{
			this.material.mainTexture = null;
		}
		if (this.scaleTransform != null)
		{
			this.scaleTransform.gameObject.SetActive(false);
		}
		this.ReleaseTexture();
	}

	protected override bool CreateTexture2D(byte[] imageBinary)
	{
		this.ReleaseTexture();
		this.texture = new Texture2D(16, 16);
		if (!this.texture.LoadImage(imageBinary))
		{
			this.ReleaseTexture();
			this.scaleTransform.gameObject.SetActive(false);
			return false;
		}
		if (this.alphaCheck)
		{
			this.isAlphaTexture_ = false;
			Color[] pixels = this.texture.GetPixels();
			for (int i = 0; i < pixels.Length; i++)
			{
				if (pixels[i].a != 1f)
				{
					this.isAlphaTexture_ = true;
					break;
				}
			}
		}
		this.material.SetFloat("_ZWrite", (float)((!this.isAlphaTexture_) ? 1 : 0));
		this.imageBinary = new byte[imageBinary.Length];
		Array.Copy(imageBinary, this.imageBinary, imageBinary.Length);
		int width = this.texture.width;
		int height = this.texture.height;
		float num = (float)Math.Min(width, height) / (float)Math.Max(width, height);
		Transform transform = this.texRenderers[0].transform;
		Vector3 one = Vector3.one;
		if (height < width)
		{
			one.z = num;
		}
		else
		{
			one.x = num;
		}
		transform.localScale = one;
		this.material.mainTexture = this.texture;
		this.scaleTransform.gameObject.SetActive(true);
		return true;
	}

	protected override void SetTextureColor(Color setColor)
	{
		if (this.material != null)
		{
			this.material.SetColor("_Color", setColor);
		}
	}

	protected override void SetScale(float scale)
	{
		if (this.scaleTransform != null)
		{
			this.scaleTransform.localScale = new Vector3(scale, scale, scale);
		}
	}

	protected override void OnSerialize(BinaryWriter writer)
	{
		writer.Write(1000);
		writer.Write(this.backRenderingType_.ToString());
		writer.Write(this.isAlphaTexture_);
		writer.Write(this.imageBinary.Length);
		writer.Write(this.imageBinary);
	}

	protected override bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader)
	{
		if (type != BasePhotoCustomObject.Type.Plane)
		{
			return false;
		}
		if (1120 < gameVersion)
		{
			int num = reader.ReadInt32();
		}
		this.backRenderingType = (PhotoCustomObjectPlane.BackRenderingType)Enum.Parse(typeof(PhotoCustomObjectPlane.BackRenderingType), reader.ReadString());
		this.isAlphaTexture_ = reader.ReadBoolean();
		int count = reader.ReadInt32();
		this.alphaCheck = false;
		base.SetTexture(reader.ReadBytes(count));
		this.alphaCheck = true;
		return true;
	}

	private void ReleaseTexture()
	{
		if (this.texture != null)
		{
			UnityEngine.Object.DestroyImmediate(this.texture);
			this.texture = null;
			this.imageBinary = null;
		}
	}

	[SerializeField]
	protected Transform scaleTransform;

	[SerializeField]
	protected GameObject subTextureObject;

	[SerializeField]
	protected Renderer[] texRenderers;

	protected Texture2D texture;

	protected Material material;

	protected byte[] imageBinary = new byte[0];

	protected PhotoCustomObjectPlane.BackRenderingType backRenderingType_;

	protected bool isAlphaTexture_;

	private bool alphaCheck = true;

	public enum BackRenderingType
	{
		Mirror,
		Flip,
		None
	}
}
