using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using wf;

public abstract class BasePhotoCustomObject : MonoBehaviour, IComparable<BasePhotoCustomObject>
{
	public static BasePhotoCustomObject CreatePrefab(GameObject parent, BasePhotoCustomObject.Type createType)
	{
		if (createType == BasePhotoCustomObject.Type.Plane)
		{
			GameObject gameObject = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Plane", false);
			return gameObject.GetComponent<BasePhotoCustomObject>();
		}
		if (createType == BasePhotoCustomObject.Type.Sphere)
		{
			GameObject gameObject2 = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Sphere", false);
			return gameObject2.GetComponent<BasePhotoCustomObject>();
		}
		return null;
	}

	public static BasePhotoCustomObject InstantiateFromFile(GameObject parent, string fileFullPath)
	{
		KeyValuePair<bool, BasePhotoCustomObject.Type> typeFromFile = BasePhotoCustomObject.GetTypeFromFile(fileFullPath);
		if (!typeFromFile.Key)
		{
			return null;
		}
		BasePhotoCustomObject.Type value = typeFromFile.Value;
		BasePhotoCustomObject basePhotoCustomObject = BasePhotoCustomObject.CreatePrefab(parent, value);
		basePhotoCustomObject.Desilialize(File.ReadAllBytes(fileFullPath));
		return basePhotoCustomObject;
	}

	public static KeyValuePair<bool, BasePhotoCustomObject.Type> GetTypeFromFile(string fileFullPath)
	{
		bool key = false;
		if (string.IsNullOrEmpty(fileFullPath) || !File.Exists(fileFullPath))
		{
			return new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, BasePhotoCustomObject.Type.Plane);
		}
		KeyValuePair<bool, BasePhotoCustomObject.Type> result;
		using (FileStream fileStream = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read))
		{
			using (BinaryReader binaryReader = new BinaryReader(fileStream))
			{
				string a = binaryReader.ReadString();
				if (a != "COM3D2_PHOTO_CUSTOM_OBJECT")
				{
					result = new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, BasePhotoCustomObject.Type.Plane);
				}
				else
				{
					int num = binaryReader.ReadInt32();
					BasePhotoCustomObject.Type value = (BasePhotoCustomObject.Type)Enum.Parse(typeof(BasePhotoCustomObject.Type), binaryReader.ReadString());
					key = true;
					result = new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, value);
				}
			}
		}
		return result;
	}

	public abstract byte[] imageBinarys { get; }

	public abstract BasePhotoCustomObject.Type type { get; }

	public abstract Texture2D mainTexture { get; }

	public float scale
	{
		get
		{
			return this.scale_;
		}
		set
		{
			this.scale_ = value;
			this.SetScale(this.scale_);
		}
	}

	public bool enabledTextureColor
	{
		get
		{
			return this.enabledTextureColor_;
		}
		set
		{
			this.enabledTextureColor_ = value;
			this.SetTextureColor((!value) ? Color.white : this.textureColor);
		}
	}

	public Color textureColor
	{
		get
		{
			return this.textureColor_;
		}
		set
		{
			bool flag = this.textureColor_ != value && this.enabledTextureColor;
			this.textureColor_ = value;
			if (flag)
			{
				this.enabledTextureColor = true;
			}
		}
	}

	public bool visible
	{
		get
		{
			return base.gameObject.activeSelf;
		}
		set
		{
			base.gameObject.SetActive(value);
		}
	}

	public virtual void Awake()
	{
		this.Clear();
	}

	public virtual void OnDestroy()
	{
	}

	public bool SetTexture(string imageFilePath)
	{
		if (string.IsNullOrEmpty(imageFilePath))
		{
			return false;
		}
		if (!File.Exists(imageFilePath))
		{
			return false;
		}
		byte[] array = File.ReadAllBytes(imageFilePath);
		return array != null && array.Length != 0 && this.SetTexture(array);
	}

	public bool SetTexture(byte[] imageFileBinary)
	{
		return imageFileBinary != null && imageFileBinary.Length > 0 && this.CreateTexture2D(imageFileBinary);
	}

	public virtual void Clear()
	{
		this.scale = 1f;
		this.enabledTextureColor = true;
		this.textureColor = Color.white;
	}

	public virtual int CompareTo(BasePhotoCustomObject comp_obj)
	{
		return ((int)comp_obj.type).CompareTo((int)this.type);
	}

	public byte[] Serialize()
	{
		byte[] result = null;
		NDebug.Assert(this.mainTexture != null, "BasePhotoCustomObject::Serialize error.\nmainTextureがnullです");
		using (MemoryStream memoryStream = new MemoryStream())
		{
			using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
			{
				binaryWriter.Write("COM3D2_PHOTO_CUSTOM_OBJECT");
				binaryWriter.Write(1170);
				binaryWriter.Write(this.type.ToString());
				binaryWriter.Write(this.scale_);
				binaryWriter.Write(this.enabledTextureColor_);
				binaryWriter.Write(this.textureColor_.a);
				binaryWriter.Write(this.textureColor_.r);
				binaryWriter.Write(this.textureColor_.g);
				binaryWriter.Write(this.textureColor_.b);
				this.OnSerialize(binaryWriter);
				result = memoryStream.ToArray();
			}
		}
		return result;
	}

	public bool Desilialize(byte[] binary)
	{
		this.Clear();
		using (MemoryStream memoryStream = new MemoryStream(binary))
		{
			using (BinaryReader binaryReader = new BinaryReader(memoryStream))
			{
				string a = binaryReader.ReadString();
				NDebug.Assert(a == "COM3D2_PHOTO_CUSTOM_OBJECT", "BasePhotoCustomObject::Desilialize error.\nヘッダーが不正です");
				int gameVersion = binaryReader.ReadInt32();
				BasePhotoCustomObject.Type type = (BasePhotoCustomObject.Type)Enum.Parse(typeof(BasePhotoCustomObject.Type), binaryReader.ReadString());
				this.scale = binaryReader.ReadSingle();
				this.enabledTextureColor = binaryReader.ReadBoolean();
				this.textureColor = new Color
				{
					a = binaryReader.ReadSingle(),
					r = binaryReader.ReadSingle(),
					g = binaryReader.ReadSingle(),
					b = binaryReader.ReadSingle()
				};
				this.OnDesilialize(gameVersion, type, binaryReader);
			}
		}
		return true;
	}

	protected abstract void OnSerialize(BinaryWriter writer);

	protected abstract bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader);

	protected abstract bool CreateTexture2D(byte[] imageFileBinary);

	protected abstract void SetTextureColor(Color setColor);

	protected abstract void SetScale(float scale);

	protected float scale_;

	protected bool enabledTextureColor_;

	protected Color textureColor_;

	public enum Type
	{
		Plane,
		Sphere
	}
}
