using UnityEngine;
using Cinemachine;

public class SplitScreenManager : MonoBehaviour
{
    private Camera mainCamera;
    private Camera mainCamera2;
    private CinemachineFreeLook vcam1;
    private CinemachineFreeLook vcam2;

    void Awake()
    {
        SetupCameras();
    }

    void SetupCameras()
    {
        mainCamera = GameObject.Find("MainCamera").GetComponent<Camera>();
        mainCamera2 = GameObject.Find("MainCamera2").GetComponent<Camera>();
        vcam1 = GameObject.Find("CinemachineCamera").GetComponent<CinemachineFreeLook>();
        vcam2 = GameObject.Find("CinemachineCamera2").GetComponent<CinemachineFreeLook>();

        if (SelectionOptions.Instance.isPlayerSelected)
        {
            ConfigureSplitScreen();
        }
        else
        {
            ConfigureSingleScreen();
        }
    }

    void ConfigureSplitScreen()
    {
        // Main Cameras Setup
        mainCamera.enabled = true;
        mainCamera2.enabled = true;
        
        mainCamera.rect = new Rect(0, 0, 0.5f, 1);
        mainCamera2.rect = new Rect(0.5f, 0, 0.5f, 1);

        // Set culling masks
        int commonLayers = LayerMask.GetMask("Default", "Ground", "UI");
        mainCamera.cullingMask = commonLayers | LayerMask.GetMask("Player One");
        mainCamera2.cullingMask = commonLayers | LayerMask.GetMask("Player Two");

        // Virtual Cameras Setup
        vcam1.gameObject.SetActive(true);
        vcam2.gameObject.SetActive(true);

        // Find and assign players
        var player = GameObject.FindGameObjectWithTag("Player");
        var enemy = GameObject.FindGameObjectWithTag("Enemy");

        if (player && enemy)
        {
            SetPlayerLayers(player, enemy);
            AssignCameraTargets(player, enemy);
        }
    }

    void ConfigureSingleScreen()
    {
        mainCamera.enabled = true;
        mainCamera.rect = new Rect(0, 0, 1, 1);
        mainCamera2.enabled = false;

        vcam1.gameObject.SetActive(true);
        vcam2.gameObject.SetActive(false);

        var player = GameObject.FindGameObjectWithTag("Player");
        if (player)
        {
            vcam1.Follow = player.transform;
            vcam1.LookAt = FindHeadTransform(player) ?? player.transform;
        }
    }

    void SetPlayerLayers(GameObject player, GameObject enemy)
    {
        SetLayerRecursively(player, LayerMask.NameToLayer("Player One"));
        SetLayerRecursively(enemy, LayerMask.NameToLayer("Player Two"));
    }

    void SetLayerRecursively(GameObject obj, int layer)
    {
        if (obj == null) return;
        obj.layer = layer;
        foreach (Transform child in obj.transform)
        {
            SetLayerRecursively(child.gameObject, layer);
        }
    }

    void AssignCameraTargets(GameObject player, GameObject enemy)
    {
        // Follow = root (orbit center), LookAt = head (aim point)
        vcam1.Follow = player.transform;
        vcam1.LookAt = FindHeadTransform(player) ?? player.transform;
        vcam2.Follow = enemy.transform;
        vcam2.LookAt = FindHeadTransform(enemy) ?? enemy.transform;
    }
    
    private Transform FindHeadTransform(GameObject character)
    {
        string[] headNames = { "Head", "head", "Bip001 Head", "mixamorig:Head", "Bip01 Head" };
        foreach (string headName in headNames)
        {
            Transform head = FindChildRecursive(character.transform, headName);
            if (head != null) return head;
        }
        return null;
    }
    
    private Transform FindChildRecursive(Transform parent, string name)
    {
        foreach (Transform child in parent)
        {
            if (child.name == name) return child;
            Transform found = FindChildRecursive(child, name);
            if (found != null) return found;
        }
        return null;
    }
}