using System;
using System.Collections;
using System.Collections.Generic;
using ScriptableObjectArchitecture;
using UnityEngine;
using UnityEngine.EventSystems;

public class BigSwitch : MonoBehaviour, IPointerClickHandler
{
    [field: SerializeField] public bool SwitchOn { get; private set; } = false;

    // [SerializeField] private GameObject _switchArt = default;

    [Header("Settings")]
    [SerializeField] private float _minAngleToRotate = 0;
    [SerializeField] private float _maxAngleToRotate = 0;
    [SerializeField] private bool _isFirstStart = true;

    [Header("Assets")]
    [SerializeField] private BoolVariable _switchOn = default;

    [Header("Refs")]
    [SerializeField] private Selectable _selectable = default;
    [SerializeField] private GameObject _switchObject = default;
    [SerializeField] private AudioSource _playOnSelect = default;

    private void Start()
    {
        SetRotation();
        _isFirstStart = false;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (_selectable.Selected)
        {
            ChangeSwitchState();
        }
        else
        {
            _selectable.Select();
            ChangeSwitchState();
        }
    }

    private void ChangeSwitchState()
    {
        SwitchOn = !SwitchOn;
        _switchOn.SetValue(SwitchOn);
        SetRotation();
    }

    private void SetRotation()
    {
        var angleMod = SwitchOn ? _maxAngleToRotate : _minAngleToRotate;

        // var currentAngles = _switchArt.transform.localEulerAngles;
        var newAngle = new Vector3(angleMod , 0, 0);
        _switchObject.transform.localEulerAngles = newAngle;
        if (!_isFirstStart)
        {
            _playOnSelect.Play();
        }
    }
}
