using Base.Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum BulletType
{
    Single = 0,//单体目标，触发后即销毁
    P﻿enetrate = 1,//穿透，可击中多个目标
    Bomb = 2,//击中目标后爆炸
}
public class Bullet : MonoBehaviour
{
    public Character owner;
    public Rigidbody rb;
    public List<BeHitTrigger> trigedObjs;
    public int damage;
    public float force;
    public bool changeHurt;
    public float repelValue;
    public BulletType bulletType;
    public bool isGetTarget = false;
    public float speed;
    public float maxFlyTime = 2f;
    public float flyTime;
    public bool isTrack;
    public Character trackTarget;

    public bool toFloat = false;

    private void Awake()
    {
        //owner = GetComponentInParent<Character>();
        rb = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        flyTime += Time.deltaTime;
        if (flyTime >= maxFlyTime)
        {
            isGetTarget = true;
            gameObject.SetActive(false);
            return;
        }
        if (isTrack && trackTarget != null && !trackTarget.isDie && trackTarget.gameObject.activeInHierarchy)
        {
            Vector3 tarDir = (trackTarget.beSearchTrigger.transform.position - transform.position).normalized;
            transform.right = Vector3.Lerp(transform.right, -tarDir, 0.2f);
            rb.velocity = Vector3.Lerp(rb.velocity, speed * tarDir, 0.2f);
        }
    }

    public void BeShoot(Character own, Vector3 shootPos, Vector3 dir, int dmg, float fc, bool hurt, float repel, bool track = false, Character target = null)
    {
        transform.position = shootPos;
        transform.right = -dir;
        gameObject.SetActive(true);
        isGetTarget = false;
        owner = own;
        damage = dmg;
        force = fc;
        changeHurt = hurt;
        repelValue = repel;
        rb.velocity = dir * speed;
        isTrack = track;
        trackTarget = target;
        flyTime = 0;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Plane"))
        {
            isGetTarget = true;
            gameObject.SetActive(false);
        }
        if (isGetTarget)
        {
            return;
        }
        BeHitTrigger hitTrigger = other.GetComponent<BeHitTrigger>();
        if (hitTrigger != null)
        {
            bool triged = false;
            for (int i = 0; i < trigedObjs.Count; i++)
            {
                if (trigedObjs[i] == hitTrigger)
                {
                    triged = true;
                    break;
                }
            }
            if (!triged)
            {
                trigedObjs.Add(hitTrigger);
                if (Util.CheckCanHit(owner.tag, hitTrigger.owner.tag) && !hitTrigger.owner.isDie)
                {
                    switch (bulletType)
                    {
                        case BulletType.Single:
                            hitTrigger.BeHit(damage, force * (-transform.right), changeHurt, repelValue);
                            isGetTarget = true;
                            gameObject.SetActive(false);
                            break;
                        case BulletType.Penetrate:
                            hitTrigger.BeHit(damage, force * (-transform.right), changeHurt, repelValue);
                            break;
                        case BulletType.Bomb:
                            break;
                        default:
                            break;
                    }
                    if (toFloat)
                    {
                        owner.attackToFloat = false;
                        toFloat = false;
                        hitTrigger.owner.GetComponent<MoveCharacter>().FloatStateOn();
                    }
                }
            }
        }
    }

    private void OnEnable()
    {
        trigedObjs.Clear();
    }
}
