using UnityEngine;
using System.Collections;
using UnityStandardAssets._2D;

public class CharacterManager : MonoBehaviour 
{
    
    public float growthScale;
    public float growthMass;
    public float shrinkScale;
    public float shrinkMass;
    public bool isDead;
    public float maxScale;
    public float MaxMass;
    public float jumpForceRate;

    private float minScale;
    private float MinMass;
    public bool help;
    public float maxJumpForce = 10000;
    public float minJumpForce = 800;

    private Rigidbody2D characterRB;
    private Vector3 characterPostion;
    private Vector3 characterScale;
    private float characterStoredMass;
    public float characterJumpforce;
    private PlatformerCharacter2D pcmd;
 
    void Start()
    {
        Init();
    }

    void Update()
    {
        characterRB.mass = Mathf.Clamp(characterRB.mass, MinMass, 300);

        Shrink();
    }

    public void Init()
    {
        characterRB = this.GetComponent<Rigidbody2D>();
        characterPostion = this.transform.position;
        characterScale = this.transform.localScale;
        pcmd = this.GetComponent<PlatformerCharacter2D>();
        characterJumpforce = pcmd.m_JumpForce;

        minScale = characterScale.y;
        MinMass = 1;
        isDead = false;
    }

    public void CheckPoint()
    {
        characterPostion = this.transform.position;
        characterScale = this.transform.localScale;
        characterStoredMass = characterRB.mass;
        characterJumpforce = pcmd.m_JumpForce;
    }

    public void ResetCharacterToLastCheckPoint()
    {
        this.transform.position = characterPostion;
        this.transform.localScale = characterScale;
        characterRB.mass = characterStoredMass;
        this.GetComponent<PlatformerCharacter2D>().m_JumpForce = characterJumpforce;
        this.GetComponent<SpriteRenderer>().flipX = false;
        pcmd.m_FacingRight = true;
    }

    public void Shrink()
    {
        if(this.transform.localScale.y <= minScale)
        {
            return;
        }
        else
        {
            this.transform.localScale -= (new Vector3(shrinkScale, shrinkScale, shrinkScale)*Time.deltaTime);

            if(characterRB.mass <= MinMass)
            {
                return;
            }
            else
            {
                characterRB.mass -= shrinkMass;

                if(characterJumpforce < minJumpForce)
                {
                    return;   
                }
                else
                {
                    help = false;
                    characterJumpforce -= jumpForceRate;
                }
                
                Debug.Log(characterJumpforce);

            }
        } 
    }

    public void Grow()
    {
        if (this.transform.localScale.y >= maxScale)
        {
            return;
        }
        else
        {
            this.transform.localScale += (new Vector3(growthScale, growthScale, growthScale)*Time.deltaTime);

            if(characterRB.mass >= MaxMass)
            {
                return;
            }
            else
            {
                characterRB.mass += growthMass;
                if (characterJumpforce > maxJumpForce)
                {
                    return;
                }
                else
                {
                    help = true;
                    characterJumpforce += jumpForceRate;
                }
            }
        }
        
    }

    public void Die()
    {
        isDead = true;
        StartCoroutine("CharacterDeath");
    }

    public IEnumerator CharacterDeath()
    {
        //CharacterDeathAnimation
        ResetCharacterToLastCheckPoint();
        return null;
    }

    public void Win()
    {
        StartCoroutine("CharacterWin");
    }

    public IEnumerator CharacterWin()
    {
        //CharacterAnim
        return null;
    }

    public void OnParticleCollision(GameObject other)
    {
        Grow();
    }

}
