using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class RepairStationBehaviour : MonoBehaviour, IInteractable, IObjectParentHolder {
    [SerializeField] private Transform _holderTransform;
    [SerializeField] private TextMeshProUGUI _countDownText;
    private Object _object;
    
    private float _timeToFix;
    private float _costToFix;

    private double _countDown;
    
    private bool _fixedCurrentObject = false;
    
    InteractionBehaviour _interactionBehaviour;
    
    

    public void Interact(InteractionBehaviour interactionBehaviour) {
        _interactionBehaviour = interactionBehaviour;
        if (interactionBehaviour.HasObject()) {
            //Player has an object
            //Check if its broken
            Object obj = interactionBehaviour.GetObject();
            if (obj.IsBroken) {
                //Object needs fixing

                HUDManager.Instance.ShowRepairHUD();
                _timeToFix = obj.ObjectSo.timeToFix;
                _costToFix = obj.ObjectSo.costToFix;
                
                //Add a random deviation to the time and cost
                _timeToFix += Random.Range(-10f, 5f);
                _costToFix += Random.Range(-2f, 15f);
                
                //Round to 1 decimal
                _timeToFix = (float) System.Math.Round(_timeToFix, 1);
                _costToFix = (float) System.Math.Round(_costToFix, 1);
                
                
                HUDManager.Instance.SetRepairHudItem(this, obj, _timeToFix, _costToFix);
            }
        } else if (_fixedCurrentObject && this.HasObject()) {
            //Player has nothing and the object is fixed
            //Give the object to the player
            _object.setObjectParent(_interactionBehaviour);
            ClearObject();
        }
    }
    
    public void ConfirmRepair() {
        _interactionBehaviour.GetObject().setObjectParent(this);
        
        _countDown = _timeToFix;
        _countDownText.gameObject.SetActive(true);  
        
        //Remove the cost from the player
        GameController.Instance.Money -= _costToFix;

        HUDManager.Instance.HideCurrentHUD();
    }
    
    private void Update() {
        if (_countDown > 0) {
            _countDown -= Time.deltaTime;
            _countDownText.text = _countDown.ToString("F2");
            if (_countDown <= 0) {
                //Repair done
                _object.IsBroken = false;
                _fixedCurrentObject = true;
                SoundFXController.Instance.PlayOvenDoneFX();
                _countDownText.gameObject.SetActive(false);
            }
        }
    }
    
    

#region Counter stuff

    public Transform GetHolderTransform() {
        return _holderTransform;
    }

    public void SetObject(Object obj) {
        _object = obj;
    }

    public Object GetObject() {
        return _object;
    }

    public void ClearObject() {
        _object = null;
    }

    public bool HasObject() {
        return _object != null;
    }

#endregion
}