using NUnit.Framework;
using UdonSharpEditor;
using UnityEditor;
using UnityEngine;

public class HandCalibrationPrefabTests
{
    private static HandCalibration LoadCalibration(GameObject root)
    {
        HandCalibration calibration = root.GetComponentInChildren<HandCalibration>(true);
        Assert.IsNotNull(calibration, "HandCalibration missing");
        UdonSharpEditorUtility.CopyUdonToProxy(calibration);
        return calibration;
    }

    [Test]
    public void MenuPrefab_HasTenChipPileButtonAndLabelWired()
    {
        GameObject root = PrefabUtility.LoadPrefabContents(HoldemPaths.Asset("Prefabs/HandCalibrationMenu.prefab"));
        try
        {
            HandCalibration calibration = LoadCalibration(root);
            Assert.IsNotNull(calibration.calibrationPile);
            Assert.IsNotNull(calibration.label);
            Assert.AreEqual(10, calibration.calibrationChips);

            PokerChipPile pile = calibration.calibrationPile;
            UdonSharpEditorUtility.CopyUdonToProxy(pile);
            Assert.AreEqual(1, pile.denominationValues.Length, "A single column is needed to calibrate against");
            Assert.AreEqual(pile.denominationValues.Length, pile.denominationPools.Length);
            Assert.AreEqual(10, pile.stackHeight);
            Assert.IsFalse(pile.supportsSelectableStackMode);

            LocalObjectPool pool = pile.denominationPools[0];
            UdonSharpEditorUtility.CopyUdonToProxy(pool);
            Assert.GreaterOrEqual(pool.Pool.Length, 10, "The pool must hold at least the ten calibration chips");

            VRCButton button = null;
            VRCButton resetButton = null;
            foreach (VRCButton candidate in root.GetComponentsInChildren<VRCButton>(true))
            {
                UdonSharpEditorUtility.CopyUdonToProxy(candidate);
                if (candidate.eventName == "_Calibrate") button = candidate;
                if (candidate.eventName == "_ResetToDefault") resetButton = candidate;
            }
            Assert.IsNotNull(resetButton, "A button must reset the calibration");
            Assert.AreSame(calibration, resetButton.targetScript);
            Assert.AreEqual(NetworkMode.Local, resetButton.networkMode);
            Assert.IsNotNull(resetButton.GetComponent<Collider>());
            Assert.IsNotNull(button);
            Assert.AreSame(calibration, button.targetScript);
            Assert.AreEqual("_Calibrate", button.eventName,
                "The underscore keeps the event from being sent over the network");
            Assert.AreEqual(NetworkMode.Local, button.networkMode);
            Assert.IsNotNull(button.GetComponent<Collider>(), "Interact needs a collider");

            // The pile sits to the left of the button as seen by the player, i.e. toward local -X.
            Assert.Less(pile.transform.localPosition.x, button.transform.localPosition.x);
        }
        finally { PrefabUtility.UnloadPrefabContents(root); }
    }

    [Test]
    public void ParentPrefab_MenuIsWiredToEveryPhysicalRaisePile()
    {
        GameObject root = PrefabUtility.LoadPrefabContents(HoldemPaths.Asset("parent.prefab"));
        try
        {
            HandCalibration calibration = LoadCalibration(root);
            PhysicalRaisePile[] expected = root.GetComponentsInChildren<PhysicalRaisePile>(true);
            Assert.Greater(expected.Length, 0);
            CollectionAssert.AreEquivalent(expected, calibration.piles);
        }
        finally { PrefabUtility.UnloadPrefabContents(root); }
    }
}
