using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
using VF.Feature.Base;
using VF.Model.Feature;

namespace VF.Feature {

public class SecurityLockBuilder : FeatureBuilder<SecurityLock> {
    [FeatureBuilderAction]
    public void Apply() {
        if (model.leftCode == 0 || model.rightCode == 0) return;

        var paramSecuritySync = controller.NewBool("SecurityLockSync", synced: true, defTrueInEditor: true);
        // This doesn't actually need synced, but vrc gets annoyed that the menu is using an unsynced param
        var paramSecurityMenu = controller.NewBool("SecurityLockMenu", synced: true);
        menu.NewMenuToggle("Security", paramSecurityMenu);
        var layer = controller.NewLayer("Security Lock");
        var entry = layer.NewState("Entry");
        var remote = layer.NewState("Remote").Move(entry, 0, -1);
        var locked = layer.NewState("Locked").Move(entry, 0, 1);
        var check = layer.NewState("Check");
        var unlocked = layer.NewState("Unlocked").Move(check, 1, 0);

        entry.TransitionsTo(remote).When(IsLocal().IsFalse());
        entry.TransitionsTo(locked).When(Always());

        locked.Drives(paramSecurityMenu, false);
        locked.Drives(paramSecuritySync, false);
        locked.TransitionsTo(check).When(paramSecurityMenu.IsTrue());

        check.TransitionsTo(unlocked).When(GestureLeft().IsEqualTo(model.leftCode).And(GestureRight().IsEqualTo(model.rightCode)));
        check.TransitionsTo(locked).When(Always());

        unlocked.Drives(paramSecuritySync, true);
        unlocked.TransitionsTo(locked).When(paramSecurityMenu.IsFalse());
    }

    public override string GetEditorTitle() {
        return "Security Lock";
    }

    public override VisualElement CreateEditor(SerializedProperty prop) {
        var content = new VisualElement();
        content.Add(new PropertyField(prop.FindPropertyRelative("leftCode"), "Left Hand Code"));
        content.Add(new PropertyField(prop.FindPropertyRelative("rightCode"), "Right Hand Code"));
        return content;
    }
    
    public override bool AvailableOnProps() {
        return false;
    }
}

}
