using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;

public class Spirits_Invisible : MonoBehaviour
{
    public Demonic demonic;
    [HideInInspector]
    public GameObject player;
    [HideInInspector]
    public PlayerController playerController;
    public float addMp;
    public InvisibleSoulCollector soulCollector;
    public GameObject soulPrefab;
    public float distance;
    public Transform body;
    public Transform shootPos;
    public SoulFollowEffect soulFollowEffect;
    public GameObject smokeEffect;
    public float interval;
    [HideInInspector]
    public Vector3 lastSmokePos;
    [HideInInspector]
    public float time;
    [HideInInspector]
    public float normalSpeed;
    public float kMp;
    public float magnification;

    private void Start()
    {
        
        player = PlayersInput.instance[demonic.playerID].gameObject;
        playerController = player.GetComponent<PlayerController>();
        playerController.uiHp.gameObject.SetActive(false);
        playerController.isInvisible = true;
        playerController.mp += addMp;
        if (playerController.floatState != 0)
        {
            playerController.floatState = 3;
            player.transform.localEulerAngles = new Vector3(0, 0, 0);
            
        }
        
        normalSpeed = playerController.moveSpeed;
        for (int i = 0;i < playerController.beTargetCharacter.Count; i++)
        {
            playerController.beTargetCharacter[i].targetCharacter = null;
        }
    }
    private void Update()
    {
        if (playerController.btnWestPress)
        {
            if (soulCollector.soulNumbers > 0)
            {
                soulCollector.soulNumbers--;
                soulFollowEffect.ShowSouls(soulCollector.soulNumbers);
                AttackJ();
            }
            else if(playerController.mp>=10)
            {
                playerController.mp -= 10;
                AttackJ();
            }
            
        }
        if (playerController.isClickBtnSouth)
        {

            time = 0;
            playerController.moveSpeed = normalSpeed * magnification;
        }
        if (playerController.isinputK)
        {
            time += Time.deltaTime;
            if (time > interval)
            {
                AttackK();
            }

        }
        else
        {
            playerController.moveSpeed = normalSpeed;
        }
        if (!playerController.isTransfiguration)
        {
            BackToPlayer();
        }
    }
    public void BackToPlayer()
    {
        playerController.ani.Play("fall",0,0);
    }
    void AttackJ()
    {
        GameObject soulObj = Instantiate(soulPrefab);
        Soul soul = soulObj.GetComponent<Soul>();
        soul.from = shootPos.transform.position;
        if (playerController.rb.velocity == Vector3.zero)
        {
            if (body.localScale.x > 0)
            {
                soul.to = transform.position + Vector3.left * distance;
            }
            else
            {
                soul.to = transform.position + Vector3.right * distance;
            }
        }
        else
        {
            soul.to = transform.position + playerController.rb.velocity.normalized * distance;
        }

        soul.isShoot = true;
    }
    void AttackK()
    {
        playerController.mp -= kMp;
        GameObject smoke = Instantiate(smokeEffect);
        smoke.transform.position = transform.position;
        lastSmokePos = transform.position;
        time = 0;

    }
}
