using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : Soul{
    public int coin, points;
    public string local;
    public bool seed, planting;
    public Transform colect;
    public GameObject tree;
    public GameObject[] locals, colliders;
    void Start(){
        life = 1;
        mana = 3;
        speed = 3f;
    }
    void Update(){
        Layer();
        if(move){
             Move();
        }
        if(colect != null || seed){
            colect.transform.position = new Vector2(Mathf.Lerp(colect.transform.position.x, transform.position.x, speed * Time.deltaTime) , Mathf.Lerp(colect.transform.position.y, transform.position.y, speed * Time.deltaTime));
        }
        if(GameManager.Instance.seconds <= 0){
            move = false;
            GameManager.Instance.life = 0;
            GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[0]);
            GetComponent<Animator>().SetBool("death", true);
            rb.velocity = Vector2.zero;
            StartCoroutine("wait");
        }
    }
    void OnTriggerEnter2D(Collider2D other) {
        if(other.tag == "Tools" && !seed && !GameManager.Instance.Ship.GetComponent<Ship>().Shipseed){
            if(colect == null){
                colect = other.transform;
                points += 10;
                other.GetComponent<Animator>().SetTrigger("take");
                GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[4]);
                if(GameManager.Instance.tutorial == 0){
                    GameManager.Instance.Tutorial();
                }
            }
        }
        
        if(other.tag == "Shoot"){
            move = false;
            GameManager.Instance.gameover = true;
            GameManager.Instance.life = 0;
            GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[0]);
            GetComponent<Animator>().SetBool("death", true);
            rb.velocity = Vector2.zero;
            Destroy(other.gameObject);
            StartCoroutine("wait");
        }
    }
    void OnTriggerStay2D(Collider2D other) {
        if(other.tag == "Dirty"){
            planting = true;
            local = other.name;
        }
    }
    void OnTriggerExit2D(Collider2D other) {
        if(other.tag == "Dirty"){
            planting = false;
        }    
    }
    void Move(){
        rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, Input.GetAxis("Vertical") * speed);
        if(rb.velocity.x != 0f || rb.velocity.y != 0f){
            GetComponent<Animator>().SetBool("walk", true);
        }else{
            GetComponent<Animator>().SetBool("walk", false);
        }
        if(rb.velocity.x < 0f){
            GetComponent<SpriteRenderer>().flipX = true;
        }
         if(rb.velocity.x > 0f){
            GetComponent<SpriteRenderer>().flipX = false;
        }
        if(Input.GetButtonDown("Jump")){
            if(seed && planting){
                GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[5]);
                if(GameManager.Instance.tutorial == 3){
                    GameManager.Instance.Tutorial();
                }
                GameManager.Instance.take++;
                seed = false;
                if(local == "dirty1"){
                    locals[0].SetActive(true);
                    colliders[0].SetActive(false);
                }
                if(local == "dirty2"){
                    locals[1].SetActive(true);
                    colliders[1].SetActive(false);
                }
                if(local == "dirty3"){
                    locals[2].SetActive(true);
                    colliders[2].SetActive(false);
                }
                GameObject t = Instantiate(tree);
                t.transform.position = new Vector2(transform.position.x, transform.position.y);
                GameManager.Instance.seeds++;
                Destroy(colect.gameObject);
            }
        }
    }
    public IEnumerator wait(){
        GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[1]);
        yield return new WaitForSeconds(2f);
        GameManager.Instance.life = 0;
    }
}