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

public class PlayerManager : MonoBehaviour
{
    #pragma warning disable 0649
    //[SerializeField] GameManager gameManager;
    #pragma warning disable 0649
    [SerializeField] LayerMask blockLayer;

    [SerializeField] private ContactFilter2D filter2d;

    //プレイヤーの現在状態を列挙型で認識
    public enum DIRECTION_TYPE{
        STOP,//止まっている
        RIGHT,//右に進んでいる
        LEFT,//左に進んでいる
    }

    //最初は止まっている
    //DIRECTION_TYPE direction = DIRECTION_TYPE.STOP;
    DIRECTION_TYPE direction;

    Rigidbody2D rigidbody2D;
    Animator animator;

    //SE
    [SerializeField] AudioClip getItemSE;
    [SerializeField] AudioClip jumpSE;
    [SerializeField] AudioClip stampSE;
    AudioSource audioSource;

    float speed;
    float JumpPower = 680;//ジャンプのための変数
    bool isDead = false;
    float x=0;
    public GameObject objPlayer;
    Vector2 playerPos;
    private void Awake()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        audioSource = GetComponent<AudioSource>();
        playerPos = objPlayer.transform.position;
    }

    private void Start(){
        print("Start");
        isDead = false;
        animator.Play("PlayerIdleAnimation");
        BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
        boxCollider2D.enabled = true;
        objPlayer.transform.position = playerPos;
    }

    void Update(){
        //入力調べる
        //Debug.Log("<color=red>"+direction+"</color>");
        //Debug.Log(x);        

        if(isDead){//死んでたらここから先処理しない
            return;
        }
        x = Input.GetAxisRaw("Horizontal");
        animator.SetFloat("speed",Mathf.Abs(x));//マイナスが入らないように絶対値(MathfのAbs)を使う
        if(x==0){
            //止まっている
            direction = DIRECTION_TYPE.STOP;
            //animator.SetFloat("Run",0);
        }
        else if(x>0){
            //右へ
            direction = DIRECTION_TYPE.RIGHT;
            //animator.SetFloat("Run",1);
        }
        else if(x<0){
            //左へ
            direction = DIRECTION_TYPE.LEFT;
            //animator.SetFloat("Run",1);
        }

        //スペース押されたらジャンプ
        if(isDead==false){//死んでたらここから先処理しない
            if(IsGround()){//地面に立ってるか
             if(Input.GetKeyDown("space") && !animator.GetBool("isJumping")){//スペースキーを押下&ジャンプ中では無い
                Jump(true);
                animator.SetBool("isJumping", true);
                }
            else{//スペースキーを押してなかったら
                animator.SetBool("isJumping",false);//ジャンプアニメしない
                }
            }            
        }
    }

    private void FixedUpdate(){
        if(isDead){//死んでたらここから先処理しない
            return;
        }        
        //現在の方向によってキャラクタを動かす
        //リジットボディで速度を与えます
        switch(direction){
            case DIRECTION_TYPE.STOP:
                speed = 0;
            break;
            case DIRECTION_TYPE.RIGHT:
                speed = 5;
                transform.localScale = new Vector3(1,1,1);
            break;
            case DIRECTION_TYPE.LEFT:
                speed = -5;
                transform.localScale = new Vector3(-1,1,1);
            break;
        }
        rigidbody2D.velocity = new Vector2(speed,rigidbody2D.velocity.y);
    
    }

    //ジャンプ
    void Jump(bool val){
        animator.SetBool("isJumping", true);
        if(val==true){
            //通常ジャンプ
            audioSource.PlayOneShot(jumpSE);
            rigidbody2D.AddForce(Vector2.up * JumpPower);
        }else{
            //敵を踏んだ時のジャンプは低くする
            audioSource.PlayOneShot(stampSE);
            rigidbody2D.AddForce(Vector2.up * 480);
        }
    }
    bool IsGround(){
        bool isTouch = rigidbody2D.IsTouching(filter2d);
        return isTouch;
    }

    //当たり判定
    private void OnTriggerEnter2D(Collider2D collision){
        if(isDead){//死んでたらここから先処理しない
            return;
        }

        if (collision.gameObject.tag == "Trap"){
            PlayerDeath();
        }

        if (collision.gameObject.tag == "Enemy"){
            //当たったのが敵だったらまずEnemyManagerを取得
           EnemyManager enemy = collision.gameObject.GetComponent<EnemyManager>();
           
            //敵に対して正面からぶつかったのか、上からぶつかったのか判別
            //上から踏んだら　双方のY座標を比較して、プレイヤーが上にいた場合は踏んだとみなす
           if(this.transform.position.y + 0.2f > enemy.transform.position.y){
            //上から踏んだらプレイヤーが跳ねる様にする
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);//yは一度0
            Jump(false);//falseは敵踏み用のジャンプ
            //敵を削除　敵のスクリプトに死ぬ関数作ってここで呼ぶ
            enemy.DestroyEnemy();
           }
           else{//横からぶつかった
                PlayerDeath();//プレイヤー削除　ゲームオーバー
           }

        }

    }

    void PlayerDeath(){
        
        isDead=true;//死亡
        //死んだら飛び上がって落ちるようにする
        rigidbody2D.velocity = new Vector2(0,0);//一旦速度ゼロに
        rigidbody2D.AddForce(Vector2.up * 680);
        animator.Play("PlayerDeathAnimation");
        //当たり判定消して画面下に落とす
        BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>();
        boxCollider2D.enabled = false;
        //gameManager.GameOver();//ゲームオーバー
        Invoke("Start", 2.5f);
    }
}
