using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class TamagushiScript : MonoBehaviour {

    public GameObject targetObj;
    public List<SpriteRenderer> P_list;
    public SpriteRenderer summon_sr;
    public Collider summon_col;
    public InputScript inputScript;

    SpriteRenderer sr;

    public float holdTime;
    public float holdTimer;
    public float summonSpeed;

    public float fadeTime;
    public float fadeTimer = 0f;

    public Rect rect;
    public Vector2 rect_center;
    public float angle;

    public Vector2 UL, UR, DL, DR;

    public Vector3 newPos;
    public float lerpTime;
    public float t = 0f;
    private Vector3 v = Vector3.zero;

    public float minDist;

    // Use this for initialization
    void Start () {

        sr = GetComponent<SpriteRenderer>();
        inputScript = GetComponent<InputScript>();

        holdTimer = 0f;
    }
	
	// Update is called once per frame
	void Update () {
        //Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector3 pos = Camera.main.ScreenToWorldPoint(rect_center);

        transform.eulerAngles = new Vector3(0f, 0f, RectAngle());

        RectUpdate();
        SummonUpdate();

        newPos = new Vector3(pos.x, pos.y, 0f);

        transform.position = Vector3.SmoothDamp(transform.position, newPos, ref v, lerpTime);
    }

    void RectUpdate()
    {
        UL = new Vector2((float)inputScript.ulx, (float)inputScript.uly);
        UR = new Vector2((float)inputScript.urx, (float)inputScript.ury);
        DL = new Vector2((float)inputScript.dlx, (float)inputScript.dly);
        DR = new Vector2((float)inputScript.drx, (float)inputScript.dry);
        rect = new Rect(Screen.width - (UL.x * 1.6f), Screen.height -  (UL.y * 1.6f), UR.x - UL.x, UL.y - DL.y);
        rect_center = rect.center;
        angle = RectAngle();
    }


    float RectAngle()
    {
        var points = new List<Vector2>() { UL, UR, DL, DR };
        float maxy = 0;
        float maxx = 0;
        for (int i = 0; i < 3; i++)
        {
            for (int j = i + 1; j < 4; j++)
            {
                var diffx = Math.Abs(points[i].x - points[j].x);
                var diffy = Math.Abs(points[i].y - points[j].y);
                maxx = Math.Max(maxx, diffx);
                maxy = Math.Max(maxy, diffy);
            }
        }

        float a;

        if (maxx < maxy)
        {
            a = 0f;
        }
        else
        {
           a = 90f;
        }

        return a;
    }

    bool Holding()
    {
        bool h = false;

        if(sr.sprite.rect.Overlaps(summon_sr.sprite.rect)) {
            h = true;
        }
        //if(sr.sprite.bounds.Intersects(summon_sr.sprite.bounds) == true) { h = true;  }

        if((transform.position - summon_sr.gameObject.transform.position).magnitude > minDist) { h = false; }
        Debug.Log(h);
        if(h == true) { fadeTimer = 0f; }
        return h;
    }

    void SummonUpdate()
    {
        if (summon_sr != null)
        {
            if (Holding() == true)
            {
                holdTimer += Time.deltaTime;
                if (summon_sr.color.a < 1f)
                {
                    summon_sr.color = new Color(summon_sr.color.r, summon_sr.color.g, summon_sr.color.b, summon_sr.color.a + summonSpeed);
                }
            }
            else
            {
                holdTimer = 0f;
                fadeTimer += Time.deltaTime;
                if (fadeTimer > fadeTime)
                {
                    summon_sr.color = new Color(summon_sr.color.r, summon_sr.color.g, summon_sr.color.b, summon_sr.color.a - 2 * summonSpeed);
                }
            }

            if (summon_sr.color.a > 0.85f)
            {
                summon_col.enabled = true;
            }
            else
            {
                summon_col.enabled = false;
            }
        }
    }

    void OnTriggerEnter(Collider col)
    {
        if(col.gameObject.tag == "targetable")
        {
            P_list.RemoveRange(0, P_list.Count); ;
            targetObj = col.gameObject;
            SpriteRenderer[] sr = targetObj.GetComponentsInChildren<SpriteRenderer>();
            foreach (SpriteRenderer s in sr) {
                if (s.gameObject.name == "Collider" || s.gameObject.name == "Calibration")
                {
                    summon_sr = s;
                    summon_col = s.gameObject.GetComponent<Collider>();
                }
                else {
                    P_list.Add(s);
                }
            }
        }
    }
}
