﻿using UnityEngine;
using System.Collections.Generic;

namespace Comm.Shoot
{
    public class ObjectShootManager : MonoBehaviour
    {       
        public GameObject Bullet;
        public float Power = 50;
        public int Int = 1;
        private bool bInit = false;
        public List<GameObject> Shooters = new List<GameObject>();

        private void OnEnable()
        {
            ShootComm.Instance.AnimatorIndex = Int;
            if (Input.touchCount > 0)
            {
                bInit = false;
            }
            else
            {
                bInit = true;
            }
        }

        private void Update()
        {
            //if (Input.GetMouseButtonDown(0))
            //{
            //    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            //    RaycastHit hitInfo;
            //    if (Physics.Raycast(ray, out hitInfo))
            //    {
            //        GameObject gameObj = hitInfo.collider.gameObject;
            //        if (Shooters != null && Shooters.Count > 0 && Shooters.Contains(gameObj))
            //        {
            //            ObjShoot(gameObj);
            //        }
            //        else
            //        {
            //            Shoot();
            //        }
            //    }
            //    else
            //    {
            //        Shoot();
            //    }
            //}

            if (Input.touchCount == 1)
            {
                Touch touch = Input.GetTouch(0);

                if (touch.phase == TouchPhase.Ended)
                {
                    //if (CommMethod.Instance.GestureType != GestureType.NONE) return;
                    //Log.Info("触摸结束事件");
                    if (!bInit)
                    {
                        bInit = true;
                        return;
                    }
                    Ray ray = Camera.main.ScreenPointToRay(touch.position);
                    RaycastHit hitInfo;
                    if (Physics.Raycast(ray, out hitInfo))
                    {
                        GameObject gameObj = hitInfo.collider.gameObject;
                        if (Shooters != null && Shooters.Count > 0 && Shooters.Contains(gameObj))
                        {
                            ObjShoot(gameObj);
                        }
                        else
                        {
                            Shoot();
                        }
                    }
                    else
                    {
                        Shoot();
                    }
                }
            }
        }

        private void Shoot()
        {
            GameObject go = ObjectPool.Instance.SpawnObject(Bullet, transform);
            if (!go.transform.GetComponent<ObjectDeleteManager>())
            {
                go.AddComponent<ObjectDeleteManager>();
            }
            Rigidbody rigidbody = go.GetComponent<Rigidbody>();
            go.transform.position = Camera.main.transform.position;
            if (rigidbody == null)
            {
                rigidbody = go.AddComponent<Rigidbody>();
            }            
            go.transform.eulerAngles = Camera.main.transform.eulerAngles;
            rigidbody.velocity = Camera.main.transform.forward * Power;
        }

        private void ObjShoot(GameObject obj)
        {
            GameObject shootPoint = null;
            var objs = obj.GetComponentsInChildren<Transform>();
            foreach (var item in objs)
            {
                if (item.gameObject.layer == 9)
                {
                    shootPoint = item.gameObject;
                    break;
                }
            }
            if (shootPoint == null)
            {
                Shoot();
            }
            else
            {
                GameObject go = ObjectPool.Instance.SpawnObject(Bullet, transform);
                if (!go.transform.GetComponent<ObjectDeleteManager>())
                {
                    go.AddComponent<ObjectDeleteManager>();
                }
                Rigidbody rigidbody = go.GetComponent<Rigidbody>();

                if (rigidbody == null)
                {
                    rigidbody = go.AddComponent<Rigidbody>();
                }
                go.transform.position = shootPoint.transform.position;
                rigidbody.velocity = shootPoint.transform.forward * Power;
            }
        }

        private void OnDestroy()
        {
            ObjectPool.Instance.Reset();
        }
    }
}
