/*================================================================
Product:    GGJ-2017
Developer:  Onur Tanrıkulu
Company:    Onur Tanrikulu
Date:       21/01/2017 03:11

Copyright (c) 2017 Onur Tanrikulu. All rights reserved.
================================================================*/

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

public class GunController : MonoBehaviour
{
   #region Fields
    private WaveManager waveManager;
    private Transform waveSpawnTransform;
    private Image powerFill;
    private float waveDelay;
    private int wavesRemaining;
    private SpriteRenderer gunSprite;
    private Sprite[] sprites;
    private int spriteId;
    private float animateDelay;
   #endregion
 
   #region Constructors
//    public GunController(Character character)
//    {
//        this.character = character;
//        gun = GameObject.Find("Gun").transform;
//        waveManager = new WaveManager();
//    }
   #endregion
 
   #region Properties
    private Character GetCharacter
    {
        get
        {
            return Character.Instance;
        }
    }

    private float GetAngleBetweenTwoPoints(Vector3 a, Vector3 b) 
    {
        return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg -180f;
    }

    private float GetPowerFill
    {
        get
        {
            float power = GetCharacter.Power.ChargedPower / 5f;

            if (power >= 1f)
            {
                return 1f;
            }
            else if (power <= 0)
            {
                return 0;
            }
            else
            {
                return power;
            }
        }
    }
   #endregion
 
   #region Methods

    private void Start()
    {
        waveManager = new WaveManager();
        powerFill = GameObject.Find("PowerFill").GetComponent<Image>();
        waveSpawnTransform = transform.GetChild(0);
        gunSprite = GetComponent<SpriteRenderer>();
        sprites = Resources.LoadAll<Sprite>("Sprites/push");
    }

    private void Update()
    {
        float dt = Time.deltaTime;
        UpdateRotation();
        UpdatePush();

        if (wavesRemaining > 0)
        {    
            waveDelay += dt;
            animateDelay += dt;

            if (animateDelay > 1 / 60)
            {
                animateDelay = 0;
                gunSprite.sprite = sprites[spriteId];

                if (spriteId == sprites.Length - 1)
                {
                    spriteId = 0;
                }
                else
                {
                    spriteId++;
                }
            }

            if (waveDelay > 0.2f)
            {
                PushWave();
                waveDelay = 0;
            }

            if (wavesRemaining <= 0)
            {
                EndWave();
            }
        }

//        if (pullWaveObject.activeInHierarchy)
//        {
//            pullWaveObject.transform.Translate(Vector2.right * 500f * Time.deltaTime);
//        }
//        if (pushWaveObject.activeInHierarchy)
//        {
//            pushWaveObject.transform.Translate(Vector2.right * 100f * Time.deltaTime);
//        }
    }

    private void UpdateRotation()
    {
        Vector2 positionOnScreen = Camera.main.WorldToViewportPoint (transform.position);
        Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
        float angle = GetAngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);

        if (GetCharacter.IsFacingRight)
        {
            if (positionOnScreen.x < mouseOnScreen.x)
            {
                transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
            }
        }
        else
        {
            if (positionOnScreen.x > mouseOnScreen.x)
            {
                transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle - 180f));
            }
        }
    }

    private void UpdatePush()
    {
        if (Input.GetMouseButtonDown(0))
        {            
            GetCharacter.Power.IsCharging = true;
            //TODO : Sound fx start
        }
        else
        {
            if (GetCharacter.Power.IsCharging)
            {
                if (GetCharacter.Power.HasOverCharged)
                {
                    StartWave();
                }
                else if (Input.GetMouseButton(0))
                {                    
                    GetCharacter.Power.ChargedPower += Time.deltaTime;
                    powerFill.fillAmount = GetPowerFill;
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    StartWave();
                }
            }
        }

    }

    private void StartWave()
    {
        powerFill.fillAmount = 0;
        GetCharacter.Power.IsCharging = false;
        wavesRemaining = (int)(GetCharacter.Power.ChargedPower * 2);
    }

    private void EndWave()
    {
        gunSprite.sprite = sprites[0];
        GetCharacter.Power.ResetCharge();
    }

    private void PushWave()
    {
        wavesRemaining--;
        Quaternion rotation = transform.rotation;

        if (Character.Instance.IsFacingRight)
        {
            
        }
        else
        {
            rotation *= Quaternion.Euler(0, 0, 180);
        }

        waveManager.CreateWave(waveSpawnTransform.position, rotation);

        //TODO: Sound fx end
        //Set power to wave and push

    }

//    private void UpdatePull()
//    {
//        if (Input.GetMouseButtonDown(1))
//        {            
//            pullPower.IsCharging = true;
//            //TODO : Sound fx start
//        }
//        else
//        {
//            if (pullPower.IsCharging)
//            {
//                if (pullPower.HasOverCharged)
//                {
//                    PullWave();
//                }
//                else if (Input.GetMouseButton(1))
//                {
//                    //TODO : Sound fx charging
//                    pullPower.ChargedPower += Time.deltaTime;
//                }
//                else if (Input.GetMouseButtonUp(1))
//                {
//                    PullWave();
//                }
//            }
//        }
//
//    }
//
//    private void PullWave()
//    {
//        pullWaveObject.SetActive(true);
//        pullWaveObject.transform.position = transform.position;
//        pullWaveObject.transform.rotation = transform.rotation;
//        pullPower.IsCharging = false;
//        Debug.Log("Push Wave with power: " + pullPower.ChargedPower);
//        //TODO: Sound fx end
//        //Set power to wave and push
//        pullPower.ResetCharge();
//    }
   #endregion
}
