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

namespace PlayerInput
{
    public class InputManager : MonoBehaviour
    {
        // TODO old
        //public GameManager GameManager;

        public PlayerController PlayerPrefab;


        // Use this for initialization
        void Start()
        {
            // TODO old
            //GameManager.OnResolutionChanged += ResolutionChanged;

            RegisterControllerToPlayer(XboxController.First, FindObjectOfType<PlayerController>(), PotentialPlayers[0]);
        }

        public enum UICorner
        {
            TopLeft,
            TopRight,
            BottomLeft,
            BottomRight
        }

        public struct PlayerInfo
        {
            public int ID;
            //public UICorner Corner;
            //public Color Color;
            //public string TextureName;
            //public string PortraitName;
        }

        public static readonly List<PlayerInfo> PotentialPlayers = new List<PlayerInfo>
        {
            new PlayerInfo { ID = 1, /*Corner = UICorner.TopLeft, Color = Color.green */ }, //, TextureName = "WizardAquaTex", PortraitName = "PlayerPortraitTeal" },
            new PlayerInfo { ID = 2, /*Corner = UICorner.TopRight, Color = Color.red */ }, //, TextureName = "WizardBlackTex", PortraitName = "PlayerPortraitBlack" },
            new PlayerInfo { ID = 3, /*Corner = UICorner.BottomLeft, Color = Color.yellow */ }, //, TextureName = "WizardRedTex", PortraitName = "PlayerPortraitRed" },
            new PlayerInfo { ID = 4, /*Corner = UICorner.BottomRight, Color = Color.cyan */ }, //, TextureName = "WizardWhiteTex", PortraitName = "PlayerPortraitWhite" },

            /*
            new PlayerInfo { ID = 1, Corner = UICorner.TopLeft, Color = new Color(104 / 255.0f, 98 / 255.0f, 182 / 255.0f) }, //, TextureName = "WizardAquaTex", PortraitName = "PlayerPortraitTeal" },
            new PlayerInfo { ID = 2, Corner = UICorner.TopRight, Color = new Color(146 / 255.0f, 55 / 255.0f, 64 / 255.0f) }, //, TextureName = "WizardBlackTex", PortraitName = "PlayerPortraitBlack" },
            new PlayerInfo { ID = 3, Corner = UICorner.BottomLeft, Color = new Color(37 / 255.0f, 122 / 255.0f, 97 / 255.0f) }, //, TextureName = "WizardRedTex", PortraitName = "PlayerPortraitRed" },
            new PlayerInfo { ID = 4, Corner = UICorner.BottomRight, Color = new Color(234 / 255.0f, 242 / 255.0f, 183 / 255.0f) }, //, TextureName = "WizardWhiteTex", PortraitName = "PlayerPortraitWhite" },
            */
        };

        private Dictionary<XboxController, InputSource<PlayerController>> controllersToInput = new Dictionary<XboxController, InputSource<PlayerController>>();
        private HashSet<PlayerInfo> usedPlayers = new HashSet<PlayerInfo>();

        public IEnumerable<PlayerController> ActivePlayers
        {
            get
            {
                return controllersToInput.Values.Select(source => source.Target);
            }
        }

        public Action OnPlayerAdded;

        // Update is called once per frame
        void Update()
        {
            for (int i = 1; i <= 4; i++)
            {
                XboxController controller = (XboxController)i;
                if (XCI.GetButtonDown(XboxButton.Start, controller))
                {
                    if (!controllersToInput.ContainsKey(controller))
                    {
                        PlayerInfo? newPlayerInfo = null;
                        foreach (PlayerInfo info in PotentialPlayers)
                        {
                            if (!usedPlayers.Contains(info))
                            {
                                newPlayerInfo = info;
                                break;
                            }
                        }
                        if (newPlayerInfo.HasValue)
                        {
                            PlayerController p = GameObject.Instantiate(PlayerPrefab).GetComponentInChildren<PlayerController>();
                            RegisterControllerToPlayer(controller, p, newPlayerInfo.Value);
                        }
                    }
                }
            }
        }

        public void RegisterControllerToPlayer(XboxController controller, PlayerController p, PlayerInfo newPlayerInfo)
        {
            XboxControllerInputSource<PlayerController> newPlayerInput = new XboxControllerInputSource<PlayerController>(controller);

            p.SetInput(newPlayerInput, newPlayerInfo);
            newPlayerInput.SetTarget(p);
            controllersToInput.Add(controller, newPlayerInput);

            //GameManager.RootDrawable.addChild(p.UI);

            //LayoutUI(p.UI, p.UICorner);

            usedPlayers.Add(newPlayerInfo);

            if (OnPlayerAdded != null) OnPlayerAdded();
        }

        private void ResolutionChanged(int width, int height)
        {
            foreach (PlayerController player in controllersToInput.Values.Select(input => input.Target))
            {
                //LayoutUI(player.UI, player.UICorner);
            }
        }

        // TODO old
        /*
        private void LayoutUI(Drawable ui, UICorner uiCorner)
        {
            switch (uiCorner)
            {
                case UICorner.TopLeft:
                    ui.X = 0;
                    ui.Y = 0;
                    break;
                case UICorner.TopRight:
                    ui.X = GameSettings.ScreenWidth;
                    ui.Y = 0;
                    break;
                case UICorner.BottomLeft:
                    ui.X = 0;
                    ui.Y = GameSettings.ScreenHeight;
                    break;
                case UICorner.BottomRight:
                    ui.X = GameSettings.ScreenWidth;
                    ui.Y = GameSettings.ScreenHeight;
                    break;
            }
        }
        */
    }
}