using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

namespace ToadieTechnika.GGJ2018Game
{
    public class LevelCard : MonoBehaviour
    {
        [SerializeField]
        Transform m_StartingPlayerPoint;

        [Header("Let the editor handle these")]
        [SerializeField]
        List<Transform> m_PlayerPoints = new List<Transform>();

        [SerializeField]
        GameObject[] m_ItemsForEnd;

        [SerializeField]
        TransmissionObject[] m_Transmissions;

        [SerializeField]
        List<Collider2D> m_Satellites = new List<Collider2D>();

        [SerializeField]
        BluetoothObject[] m_BluetoothObjects;

        #region Properties
        public List<Collider2D> Satellites
        {
            get { return m_Satellites; }
        }

        public List<Transform> PlayerPoints
        {
            get { return m_PlayerPoints; }
        }

        public int GetItemsForEndLength
        {
            get { return m_ItemsForEnd.Length; }
        }

        public TransmissionObject[] Transmissions
        {
            get { return m_Transmissions; }
            set { m_Transmissions = value; }
        }

        public BluetoothObject[] BluetoothObjects
        {
            get { return m_BluetoothObjects; }
            set { m_BluetoothObjects = value; }
        }
        #endregion

        #region Functions
        public void ResetLevelCard()
        {
            #region Player points
            if (m_StartingPlayerPoint == null)
            {
                Debug.LogError("Insert a starting point first!");
                return;
            }

            //Normalize Level Card
            GameObject[] _FoundPlayerPoints = GameObject.FindGameObjectsWithTag("PlayerPoint");
            m_PlayerPoints.Clear();
            for (int i = 0; i < _FoundPlayerPoints.Length; i++)
            {
                if (_FoundPlayerPoints[i].layer != LayerMask.NameToLayer("StartingPlayer"))
                {
                    m_PlayerPoints.Add(_FoundPlayerPoints[i].transform);
                    Debug.Log("Placed!");
                }
            }

            //Add the first one at the top!
            m_PlayerPoints.Insert(0, m_StartingPlayerPoint);
            #endregion

            //Get Items for End
            m_ItemsForEnd = GameObject.FindGameObjectsWithTag("CollectForEnd");

            //Transmissions
            m_Transmissions = FindObjectsOfType<TransmissionObject>();

            //Satellites
            m_Satellites.Clear();
            GameObject[] _SatelliteObjects = GameObject.FindGameObjectsWithTag("Satellite");
            foreach (GameObject _Satellite in _SatelliteObjects)
                m_Satellites.Add(_Satellite.GetComponent<Collider2D>());

            //Bluetooth objects
            m_BluetoothObjects = FindObjectsOfType<BluetoothObject>();

            Debug.Log("Level Card Complete: " + gameObject.name);
        }

        public void NormalizeLevel()
        {
            foreach (GameObject _ItemForEnd in m_ItemsForEnd)
                _ItemForEnd.SetActive(true);

            GameplayController.instance.CurrentTransmissions = m_Transmissions;
            foreach (TransmissionObject _Transmission in m_Transmissions)
            {
                if (_Transmission.IsInit)
                    _Transmission.transform.position = _Transmission.NormalPos;
            }
        }
        #endregion

        #if UNITY_EDITOR
        void OnDrawGizmos()
        {
            foreach (Transform _PlayerPoint in m_PlayerPoints)
            {
                if (_PlayerPoint != null)
                {
                    Gizmos.color = Color.blue;
                    Gizmos.DrawWireCube(_PlayerPoint.position, Vector3.one);
                }
            }
        }
        #endif
    }

    [System.Serializable]
    public struct CatchedObject
    {
        [SerializeField]
        GameObject m_CatchedGameObject;

        [SerializeField]
        Vector2 m_FixedPosition;

        #region Properties
        public GameObject CatchedGameObject
        {
            get { return m_CatchedGameObject; }
            set { m_CatchedGameObject = value; }
        }

        public Vector2 FixedPosition
        {
            get { return m_FixedPosition; }
            set { m_FixedPosition = value; }
        }
        #endregion

        public CatchedObject(GameObject _CatchedGameObject, Vector2 _FixedPosition)
        {
            m_CatchedGameObject = _CatchedGameObject;
            m_FixedPosition = _FixedPosition;
        }
    }

}