/***********************************************
				EasyTouch Controls
	Copyright © 2014-2015 The Hedgehog Team
  http://www.blitz3dfr.com/teamtalk/index.php
		
	  The.Hedgehog.Team@gmail.com
		
**********************************************/
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;

[System.Serializable]
public abstract class ETCBase : MonoBehaviour {

	public enum ControlType {Joystick, TouchPad, DPad, Button};
	public enum RectAnchor { UserDefined,BottomLeft,BottomCenter,BottonRight,CenterLeft,Center,CenterRight,TopLeft,TopCenter, TopRight};
	public enum DPadAxis{ Two_Axis, Four_Axis };

	protected RectTransform cachedRectTransform;	
	protected Canvas cachedRootCanvas;

	[SerializeField]
	protected RectAnchor _anchor;
	public RectAnchor anchor {
		get {
			return _anchor;
		}
		set {
			if (value != _anchor){
				_anchor = value;
				SetAnchorPosition();
			}
		}
	}
	
	[SerializeField]
	protected Vector2 _anchorOffet;
	public Vector2 anchorOffet {
		get {
			return _anchorOffet;
		}
		set {
			if (value != _anchorOffet){
				_anchorOffet = value;
				SetAnchorPosition();
			}
		}
	}
	
	[SerializeField]
	protected bool _visible;
	public bool visible {
		get {
			return _visible;
		}
		set {
			if (value != _visible){
				_visible = value;
				SetVisible();
			}
		}
	}
	
	[SerializeField]
	protected bool _activated;
	public bool activated {
		get {
			return _activated;
		}
		set {
			if (value != _activated){
				_activated = value;
				SetActivated();
			}
		}
	}

	public DPadAxis dPadAxisCount;

	public bool useFixedUpdate;

	public bool isOnDrag;
	public bool isSwipeIn;
	public bool isSwipeOut;

	public bool showPSInspector;
	public bool showSpriteInspector;
	public bool showEventInspector;
	public bool showBehaviourInspector;
	public bool showAxesInspector;
	public bool showTouchEventInspector;
	public bool showDownEventInspector;
	public bool showPressEventInspector;


	protected virtual void Awake(){
		cachedRectTransform = transform as RectTransform;
		cachedRootCanvas = transform.parent.GetComponent<Canvas>();
		ETCInput.instance.RegisterControl( this);
	}
	
	protected virtual void SetVisible(){

	}

	protected virtual void SetActivated(){

	}

	public void SetAnchorPosition(){
		
		switch (_anchor){
		case RectAnchor.TopLeft:
			this.rectTransform().anchorMin = new Vector2(0,1);
			this.rectTransform().anchorMax = new Vector2(0,1);
			this.rectTransform().anchoredPosition = new Vector2( this.rectTransform().sizeDelta.x/2f + _anchorOffet.x, -this.rectTransform().sizeDelta.y/2f - _anchorOffet.y);
			break;
		case RectAnchor.TopCenter:
			this.rectTransform().anchorMin = new Vector2(0.5f,1);
			this.rectTransform().anchorMax = new Vector2(0.5f,1);
			this.rectTransform().anchoredPosition = new Vector2(  _anchorOffet.x, -this.rectTransform().sizeDelta.y/2f - _anchorOffet.y);
			break;
		case RectAnchor.TopRight:
			this.rectTransform().anchorMin = new Vector2(1,1);
			this.rectTransform().anchorMax = new Vector2(1,1);
			this.rectTransform().anchoredPosition = new Vector2( -this.rectTransform().sizeDelta.x/2f - _anchorOffet.x, -this.rectTransform().sizeDelta.y/2f - _anchorOffet.y);
			break;
			
		case RectAnchor.CenterLeft:
			this.rectTransform().anchorMin = new Vector2(0,0.5f);
			this.rectTransform().anchorMax = new Vector2(0,0.5f);
			this.rectTransform().anchoredPosition = new Vector2( this.rectTransform().sizeDelta.x/2f + _anchorOffet.x, _anchorOffet.y);
			break;
			
		case RectAnchor.Center:
			this.rectTransform().anchorMin = new Vector2(0.5f,0.5f);
			this.rectTransform().anchorMax = new Vector2(0.5f,0.5f);
			this.rectTransform().anchoredPosition = new Vector2(  _anchorOffet.x, _anchorOffet.y);
			break;
			
		case RectAnchor.CenterRight:
			this.rectTransform().anchorMin = new Vector2(1,0.5f);
			this.rectTransform().anchorMax = new Vector2(1,0.5f);
			this.rectTransform().anchoredPosition = new Vector2( -this.rectTransform().sizeDelta.x/2f -  _anchorOffet.x, _anchorOffet.y);
			break; 
			
		case RectAnchor.BottomLeft:
			this.rectTransform().anchorMin = new Vector2(0,0);
			this.rectTransform().anchorMax = new Vector2(0,0);
			this.rectTransform().anchoredPosition = new Vector2( this.rectTransform().sizeDelta.x/2f + _anchorOffet.x, this.rectTransform().sizeDelta.y/2f + _anchorOffet.y);
			break;
		case RectAnchor.BottomCenter:
			this.rectTransform().anchorMin = new Vector2(0.5f,0);
			this.rectTransform().anchorMax = new Vector2(0.5f,0);
			this.rectTransform().anchoredPosition = new Vector2(  _anchorOffet.x, this.rectTransform().sizeDelta.y/2f + _anchorOffet.y);
			break;
		case RectAnchor.BottonRight:
			this.rectTransform().anchorMin = new Vector2(1,0);
			this.rectTransform().anchorMax = new Vector2(1,0);
			this.rectTransform().anchoredPosition = new Vector2( -this.rectTransform().sizeDelta.x/2f - _anchorOffet.x, this.rectTransform().sizeDelta.y/2f + _anchorOffet.y);
			break;
		}
		
	}
}
