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

public class Ship : Layers{
    public GameObject value, Oseed, player, Enemy, glow;
    public float time, op;
    public bool Shipseed, entryShip, up, opp;
    void OnTriggerStay2D(Collider2D other) {
        if(other.tag == "Player" && Shipseed == true && time <= 0f && other.GetComponent<Player>().colect == null){
            other.GetComponent<Player>().seed = true;
            Shipseed = false;
            GameObject sd = Instantiate(Oseed);
            sd.transform.position = transform.position;
            other.GetComponent<Player>().colect = sd.transform;
            transform.GetChild(5).gameObject.SetActive(false);
            transform.GetChild(3).gameObject.SetActive(false);
            GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[4]);
            value.transform.localScale = new Vector2(0f, value.transform.localScale.y);
            if(GameManager.Instance.tutorial == 2){
                GameManager.Instance.Tutorial();
            }
        }
    }
    void OnTriggerEnter2D(Collider2D other) {
        if(other.tag == "Player" && other.GetComponent<Player>().colect != null && !other.GetComponent<Player>().seed){
            time = 4f;
            value.transform.localScale = new Vector2(0f, value.transform.localScale.y);
            Shipseed = true;
            opp = true;
            transform.GetChild(5).gameObject.SetActive(true);
            if(GameManager.Instance.tutorial == 1){
                GameManager.Instance.Tutorial();
            }
            Destroy(other.GetComponent<Player>().colect.gameObject);
        }
        if(other.tag == "Player"){
            if(GameManager.Instance.win){
                up = true;
                GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[6]);
                GameManager.Instance.EnterShip = true;
                PlayerPrefs.SetInt("initial", 1);
            }
        }
    }
    void Update(){
        if(glow.GetComponent<SpriteRenderer>().color.a < 1f  && !opp){
            op += 1f * Time.deltaTime;
            glow.GetComponent<SpriteRenderer>().color = new Color(1f,1f,1f, op);
        }
        if(glow.GetComponent<SpriteRenderer>().color.a >= 1f  && !opp){
            opp = true;
        }
        if(glow.GetComponent<SpriteRenderer>().color.a > 0f  && opp){
            op += -1f * Time.deltaTime;
            glow.GetComponent<SpriteRenderer>().color = new Color(1f,1f,1f, op);
        }
        if(glow.GetComponent<SpriteRenderer>().color.a >= 0f  && opp){
            opp = false;
        }
        
        Layer();
        if(up){
            transform.GetChild(2).gameObject.SetActive(true);
            transform.position = new Vector2(transform.position.x, transform.position.y + 0.8f * Time.deltaTime);
            player.SetActive(false);
            player.GetComponent<Player>().move = false;
            if(transform.position.y >= 10.45){
                SceneManager.LoadScene("Menu");
            }
        }
        if(transform.position.y > 3.28f && !entryShip){
            transform.position = new Vector2(transform.position.x, transform.position.y - 0.8f * Time.deltaTime);
        }else{
            if(!entryShip){
                transform.GetChild(2).gameObject.SetActive(false);
                if(!player.activeInHierarchy){
                    player.SetActive(true);
                    GameManager.Instance.time.gameObject.SetActive(true);
                    GameManager.Instance.timerslider.gameObject.SetActive(true);
                    GameManager.Instance.btn_pause.SetActive(true);
                    GameManager.Instance.arrows[0].SetActive(true);
                    StartCoroutine("push");
                    GameManager.Instance.value.enabled = true;
                    GameManager.Instance.bar.enabled = true;
                }
            }
        }
        if(time > 0f && Shipseed){
            time -= 1f * Time.deltaTime;
            if(value.transform.localScale.x < 9.993882f){
                transform.GetChild(3).gameObject.SetActive(false);
                value.transform.localScale = new Vector2(value.transform.localScale.x + 2.5f * Time.deltaTime, value.transform.localScale.y);
            }
        }
        if(time < 0f && Shipseed){
            time = 0f;
            GameManager.Instance.audios.PlayOneShot(GameManager.Instance.clips[3]);
            transform.GetChild(3).gameObject.SetActive(true);
            value.transform.localScale = new Vector2(9.993882f, value.transform.localScale.y);
        }
    }
    public IEnumerator push(){ 
        yield return new WaitForSeconds(0.5f);
        Time.timeScale = 0f;
        GameManager.Instance.containerTutorial.SetActive(true);
        entryShip = true;
    }
}