/*
http://www.cgsoso.com/forum-211-1.html

CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!

CGSOSO 主打游戏开发,影视设计等CG资源素材。

插件如若商用,请务必官网购买!

daily assets update for try.

U should buy the asset from home store if u use it in your project!
*/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[Serializable]
//public class PipeSprites
//{
//    public List<Sprite> sprites;
//}
public class Cell : MonoBehaviour
{
	public Vector2Int pos;

	public PipeColor defaultColor;

	public int rightRotationValue;

	[Header("PipeDetail")]
	public RectTransform pipeParent;

	public CellType pipeCellType;

	public List<Pipe> pipes;

	public List<Sprite> pipeSprites;
	//public List<PipeSprites> _PipeSprites;
	public int correctRotateDelta = 4;

	[HideInInspector]
	public bool redundant;

	private bool _isHint;

	public int _rotationValue;
	private object pipe;
	public CellKind cellKind;



	public bool IsHint
	{
		get
		{
			return _isHint;
		}
		set
		{
			if (value)
			{
				RotationValue = rightRotationValue;
				ApplyRotationOnImage(0.05f);
				GetComponent<Image>().color = ColorManager.intance.hintCellBGColor;
			}
			else
			{
				GetComponent<Image>().color = ColorManager.intance.normalCellBGColor;
			}
			_isHint = value;
		}
	}

	public int RotationValue
	{
		get
		{
			return _rotationValue;
		}
		set
		{
			if (!IsHint)
			{
				int num = (value < 4) ? value : 0;
				num = (_rotationValue = ((num >= 0) ? num : 3));
				pipes.ForEach(delegate (Pipe obj)
				{
					obj.SetPipeRotation(_rotationValue);
					SetCellFlowSide(cellKind);
				});
			}
		}
	}

	public Cell RightCell
	{
		get
		{
			Vector2Int v = pos + Vector2Int.right;
			if (!IsValidIndex(v))
			{
				return null;
			}
			return GetCellFromVector(v);
		}
	}

	public Cell LeftCell
	{
		get
		{
			Vector2Int v = pos + Vector2Int.left;
			if (!IsValidIndex(v))
			{
				return null;
			}
			return GetCellFromVector(v);
		}
	}

	public Cell TopCell
	{
		get
		{
			Vector2Int v = pos + Vector2Int.down;
			if (!IsValidIndex(v))
			{
				return null;
			}
			return GetCellFromVector(v);
		}
	}

	public Cell BottomCell
	{
		get
		{
			Vector2Int v = pos + Vector2Int.up;
			if (!IsValidIndex(v))
			{
				return null;
			}
			return GetCellFromVector(v);
		}
	}

	public void SetLevelData(LevelCellData lcd)
	{
		defaultColor = lcd.DefaultColor;
		if (pipeCellType != 0)
		{
			GetComponent<Button>().onClick.AddListener(delegate
			{
				if (GamePlayManager.instance != null)
				{
					GamePlayManager.instance.OnButtonClick(this);
				}
			});
		}
		rightRotationValue = lcd.RightRotationValue;
		RotationValue = lcd.CurrentRotationValue;
		ApplyRotationOnImage();
		UpdatePipeColor();
		IsHint = false;
		redundant = lcd.Redundant;
	}

	public void UpdatePipeColor()
	{

		if (pipeCellType == CellType.Start)
		{
			pipes.ForEach(delegate (Pipe p)
			{
				switch (defaultColor)
				{
					case PipeColor.None:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.None;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.None;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.None;

						});
						break;
					case PipeColor.A:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Red;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Red;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Red;

						});
						break;
					case PipeColor.B:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Blue;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Blue;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Blue;

						});
						break;
					case PipeColor.C:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Yellow;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Yellow;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Yellow;

						});
						break;
					case PipeColor.AB:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Purple;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Purple;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Purple;

						});
						break;
					case PipeColor.AC:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Orange;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Orange;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Orange;


						});
						break;
					case PipeColor.BC:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Green;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Green;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Green;


						});
						break;
					case PipeColor.ABC:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Brown;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Brown;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Brown;


						});
						break;
					default:

						break;
				}

			});

		}



		if (pipeCellType == CellType.Middle)
		{
			pipes.ForEach(delegate (Pipe p)
			{
				p.pipeImage.sprite = pipeSprites[(int)ColorManager.MixPipeColor(p.fillColor)];

				switch (ColorManager.MixPipeColor(p.fillColor))
				{
					case PipeColor.None:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.None;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.None;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.None;

						});
						break;
					case PipeColor.A:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Red;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Red;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Red;

						});
						break;
					case PipeColor.B:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Blue;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Blue;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Blue;

						});
						break;
					case PipeColor.C:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Yellow;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Yellow;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Yellow;

						});
						break;
                    case PipeColor.AB:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Purple;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Purple;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Purple;

						});
						break;
					case PipeColor.AC:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Orange;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Orange;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Orange;


						});
						break;
					case PipeColor.BC:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Green;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Green;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Green;


						});
						break;
					case PipeColor.ABC:
						pipes.ForEach(delegate (Pipe obj)
						{
							obj.Img_1.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Brown;
							obj.Img_2.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Brown;
							obj.Img_3.transform.GetChild(0).GetComponent<Image>().color = ColorManager.intance.Brown;


						});
						break;
					default:

						break;
                }


            });
		}
		else
		{
			pipes.ForEach(delegate (Pipe p)
			{
				p.pipeImage.sprite = pipeSprites[(int)defaultColor];
			});
		}
	}

	public void ApplyRotationOnImage(float t = 0f)
	{
		LeanTween.rotateZ(pipeParent.gameObject, (float)_rotationValue * -90f, t);
	}

	private bool IsValidIndex(Vector2Int v)
	{
		if (v.x < 0 || v.y < 0 || v.x >= GameManager.currentLevel.column || v.y >= GameManager.currentLevel.row)
		{
			return false;
		}
		Cell cellFromVector = GetCellFromVector(v);
		if (cellFromVector.pipeCellType == CellType.Blank)
		{
			return false;
		}
		return true;
	}

	private Cell GetCellFromVector(Vector2Int v)
	{
		int index = v.y * GameManager.currentLevel.column + v.x;
		return GamePlayManager.instance.allCellList[index];
	}

	public bool HasSide(Side side)
	{
		foreach (Pipe pipe in pipes)
		{
			if (pipe.R && side == Side.R)
			{
				return true;
			}
			if (pipe.L && side == Side.L)
			{
				return true;
			}
			if (pipe.T && side == Side.T)
			{
				return true;
			}
			if (pipe.B && side == Side.B)
			{
				return true;
			}
		}
		return false;
	}

	public bool HasAnyColor()
	{
		foreach (Pipe pipe in pipes)
		{
			if (pipe.fillColor.Count > 0)
			{
				return true;
			}
		}
		return false;
	}

	public bool IsInRighRotation()
	{
		return (Mathf.Max(RotationValue, rightRotationValue) - Mathf.Min(RotationValue, rightRotationValue)) % correctRotateDelta == 0;
	}

	public void RemoveAllPipeColor()
	{
		pipes.ForEach(delegate (Pipe obj)
		{
			obj.RemoveAll();
		});

		// Reset Pipe Leaking 
		pipes.ForEach(delegate (Pipe obj)
		{
			obj.BottomLeak = false;
			obj.LeftLeak = false;
			obj.RightLeak = false;
			obj.TopLeak = false;


			if(obj.Img_1)
				obj.Img_1.gameObject.SetActive(false);
			if(obj.Img_2)
				obj.Img_2.gameObject.SetActive(false);
			if(obj.Img_3)
				obj.Img_3.gameObject.SetActive(false);

			

			

		});
	}

	public void FillColor(List<PipeColor> c, Cell source, Side sourceSide)
	{
		if (pipeCellType != CellType.Start)
		{
			foreach (Pipe pipe in pipes)
			{
				if ((pipe.L && sourceSide == Side.R) || (pipe.R && sourceSide == Side.L) || (pipe.T && sourceSide == Side.B) || (pipe.B && sourceSide == Side.T))
				{
					if (pipe.AddColor(c) == 0)
					{
						
						break;
					}
					if (pipe.L && LeftCell != null && LeftCell != source)
					{
						LeftCell.FillColor(c, this, Side.L);

					}




					if (pipe.L)
					{

						if (LeftCell != null)
						{
							foreach (Pipe leftCellPipe in LeftCell.pipes)
							{
								if (leftCellPipe.R)
								{
									pipe.LeftLeak = false;

									if (pipe.leakL != null)
									{
										pipe.leakL.gameObject.SetActive(false);
									}
									break;
								}
								else
								{
									pipe.LeftLeak = true;
									if (pipe.leakL != null)
									{
										pipe.leakL.gameObject.SetActive(true);
									}
								}
							}
						}
						else
						{
							pipe.LeftLeak = true;
							if (pipe.leakL != null)
							{
								pipe.leakL.gameObject.SetActive(true);
							}
						}


						//if (LeftCell == null)
						//{
						//	pipe.LeftLeak = true;
						//}

						//else if (LeftCell.pipes[0].R)
						//{
						//	pipe.LeftLeak = false;
						//}
						//else
						//{
						//	pipe.LeftLeak = true;
						//}
					}

					if (pipe.R && RightCell != null && RightCell != source)
					{
						RightCell.FillColor(c, this, Side.R);

						
					}

					if (pipe.R)
					{

						if (RightCell != null)
						{
							foreach (Pipe rightCellPipe in RightCell.pipes)
							{
								if (rightCellPipe.L)
								{
									pipe.RightLeak = false;
									if (pipe.leakR != null)
									{
										pipe.leakR.gameObject.SetActive(false);
									}
									break;
								}
								else
								{
									pipe.RightLeak = true;
									if (pipe.leakR != null)
									{
										pipe.leakR.gameObject.SetActive(true);
									}
								}
							}
						}
						else
						{
							pipe.RightLeak = true;
							if (pipe.leakR != null)
							{
                                pipe.leakR.gameObject.SetActive(true);
                            }
						}




					}



					if (pipe.T && TopCell != null && TopCell != source)
					{
						TopCell.FillColor(c, this, Side.T);

						
					}

					if (pipe.T)
					{


						if (TopCell != null)
						{
							foreach (Pipe topCellPipe in TopCell.pipes)
							{
								if (topCellPipe.B)
								{
									pipe.TopLeak = false;
									if (pipe.leakT != null)
									{
										pipe.leakT.gameObject.SetActive(false);
									}
									break;
								}
								else
								{
									pipe.TopLeak = true;
									if (pipe.leakT != null)
									{
										pipe.leakT.gameObject.SetActive(true);
									}
								}
							}
						}
						else
						{
							pipe.TopLeak = true;
							if (pipe.leakT != null)
							{
								pipe.leakT.gameObject.SetActive(true);
							}
						}

						
					}





					if (pipe.B && BottomCell != null && BottomCell != source)
					{
						BottomCell.FillColor(c, this, Side.B);

						
					}



					if (pipe.B)
					{

						if (BottomCell != null)
						{
							foreach (Pipe bottomCellPipe in BottomCell.pipes)
							{
								if (bottomCellPipe.T)
								{
									pipe.BottomLeak = false;
									if (pipe.leakB != null)
									{
										pipe.leakB.gameObject.SetActive(false);
									}
									break;
								}
								else
								{
									pipe.BottomLeak = true;
									if (pipe.leakB != null)
									{
										pipe.leakB.gameObject.SetActive(true);
									}
								}
							}
						}
						else
						{
							pipe.BottomLeak = true;
							if (pipe.leakB != null)
							{
								pipe.leakB.gameObject.SetActive(true);
							}
						}


					}




				}
			}



		}
	}


	
	
	public void SetCellFlowSide(CellKind cellKind)
	{
		

		

		switch (cellKind)
		{
			

			case CellKind.Single:

				if (_rotationValue ==0)
				{
					pipes[0].leakB = pipes[0].Img_1.gameObject;
					
				}
				else if (_rotationValue == 1)
				{
					pipes[0].leakL = pipes[0].Img_1.gameObject;
				}
				else if (_rotationValue == 2)
				{
					pipes[0].leakT = pipes[0].Img_1.gameObject;
				}
				else if (_rotationValue == 3)
				{
					pipes[0].leakR = pipes[0].Img_1.gameObject;
				}

				break;

			case CellKind.StraightTwoFace:

				if (_rotationValue == 0)
				{
					pipes[0].leakB = pipes[0].Img_1.gameObject;
					pipes[0].leakT = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 1)
				{
					pipes[0].leakL = pipes[0].Img_1.gameObject;
					pipes[0].leakR = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 2)
				{
					pipes[0].leakT = pipes[0].Img_1.gameObject;
					pipes[0].leakB = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 3)
				{
					pipes[0].leakR = pipes[0].Img_1.gameObject;
					pipes[0].leakL = pipes[0].Img_2.gameObject;

				}


				break;
			case CellKind.CurvedTwoFace:

				if (_rotationValue == 0)
				{
					pipes[0].leakB = pipes[0].Img_1.gameObject;
					pipes[0].leakR = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 1)
				{
					pipes[0].leakL = pipes[0].Img_1.gameObject;
					pipes[0].leakB = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 2)
				{
					pipes[0].leakT = pipes[0].Img_1.gameObject;
					pipes[0].leakL = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 3)
				{
					pipes[0].leakR = pipes[0].Img_1.gameObject;
					pipes[0].leakT = pipes[0].Img_2.gameObject;

				}


				break;
			case CellKind.ThreeFace:

				if (_rotationValue == 0)
				{
					pipes[0].leakB = pipes[0].Img_1.gameObject;
					pipes[0].leakL = pipes[0].Img_2.gameObject;
					pipes[0].leakR = pipes[0].Img_3.gameObject;

				}
				else if (_rotationValue == 1)
				{
					pipes[0].leakL = pipes[0].Img_1.gameObject;
					pipes[0].leakB = pipes[0].Img_3.gameObject;
					pipes[0].leakT = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 2)
				{
					pipes[0].leakT = pipes[0].Img_1.gameObject;
					pipes[0].leakL = pipes[0].Img_3.gameObject;
					pipes[0].leakR = pipes[0].Img_2.gameObject;


				}
				else if (_rotationValue == 3)
				{
					pipes[0].leakR = pipes[0].Img_1.gameObject;
					pipes[0].leakB = pipes[0].Img_2.gameObject;
					pipes[0].leakT = pipes[0].Img_3.gameObject;
				}


				break;
			case CellKind.StraightDualTwoFace:

				if (_rotationValue == 0)
				{
					pipes[0].leakB = pipes[0].Img_1.gameObject;
					pipes[0].leakT = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 1)
				{
					pipes[0].leakL = pipes[0].Img_1.gameObject;
					pipes[0].leakR = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 2)
				{
					pipes[0].leakT = pipes[0].Img_1.gameObject;
					pipes[0].leakB = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 3)
				{
					pipes[0].leakR = pipes[0].Img_1.gameObject;
					pipes[0].leakL = pipes[0].Img_2.gameObject;

				}

				//////////////////////////////////////////////////////////
				
				if (_rotationValue == 0)
				{
					pipes[1].leakL = pipes[1].Img_1.gameObject;
					pipes[1].leakR = pipes[1].Img_2.gameObject;

				}
				else if (_rotationValue == 1)
				{
					pipes[1].leakT = pipes[1].Img_1.gameObject;
					pipes[1].leakB = pipes[1].Img_2.gameObject;

				}
				else if (_rotationValue == 2)
				{
					pipes[1].leakL = pipes[1].Img_2.gameObject;
					pipes[1].leakR = pipes[1].Img_1.gameObject;

				}
				else if (_rotationValue == 3)
				{
					pipes[1].leakT = pipes[1].Img_2.gameObject;
					pipes[1].leakB = pipes[1].Img_1.gameObject;

				}


				break;
			case CellKind.CurvedDualTwoFace:

				if (_rotationValue == 0)
				{
					pipes[0].leakR = pipes[0].Img_1.gameObject;
					pipes[0].leakB = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 1)
				{
					pipes[0].leakB = pipes[0].Img_1.gameObject;
					pipes[0].leakL = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 2)
				{
					pipes[0].leakL = pipes[0].Img_1.gameObject;
					pipes[0].leakT = pipes[0].Img_2.gameObject;

				}
				else if (_rotationValue == 3)
				{
					pipes[0].leakT = pipes[0].Img_1.gameObject;
					pipes[0].leakR = pipes[0].Img_2.gameObject;

				}

				//////////////////////////////////////////////////
				if (_rotationValue == 0)
				{
					pipes[1].leakT = pipes[1].Img_1.gameObject;
					pipes[1].leakL = pipes[1].Img_2.gameObject;

				}
				else if (_rotationValue == 1)
				{
					pipes[1].leakR = pipes[1].Img_1.gameObject;
					pipes[1].leakT = pipes[1].Img_2.gameObject;

				}
				else if (_rotationValue == 2)
				{
					pipes[1].leakB = pipes[1].Img_1.gameObject;
					pipes[1].leakR = pipes[1].Img_2.gameObject;

				}
				else if (_rotationValue == 3)
				{
					pipes[1].leakL = pipes[1].Img_1.gameObject;
					pipes[1].leakB = pipes[1].Img_2.gameObject;

				}
				break;



			default:
				break;

		}
	}



}