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

public class BettingSpace : MonoBehaviour
{
    public float ratio = 1;
    EventTrigger eventTrigger;
    Image image;
    Color defaultColor;
    RectTransform rect;
    public Vector3 curPosition;
    public float distance;

    public int Number;
    void Start()
    {
        rect = GetComponent<RectTransform>();
        image = GetComponent<Image>();
        defaultColor = image.color;
        eventTrigger = GetComponent<EventTrigger>();

        EventTrigger.Entry OnEnter = new EventTrigger.Entry();
        OnEnter.eventID = EventTriggerType.PointerEnter;
        OnEnter.callback.AddListener(onEnter);

        EventTrigger.Entry OnExit = new EventTrigger.Entry();
        OnExit.eventID = EventTriggerType.PointerExit;
        OnExit.callback.AddListener(onExit);

        EventTrigger.Entry OnClick = new EventTrigger.Entry();
        OnClick.eventID = EventTriggerType.PointerClick;
        OnClick.callback.AddListener(onClick);

        eventTrigger.triggers.Add(OnEnter);
        eventTrigger.triggers.Add(OnExit);
        eventTrigger.triggers.Add(OnClick);

        curPosition = transform.position;

        RouletteManager.RegisterBettingSpace(this);
    }
    Color changedColor {get { return new Color(defaultColor.r,defaultColor.g,defaultColor.b,defaultColor.a*0.5f);}}
    void onEnter(BaseEventData e){
        if(RouletteManager.spinning){return;}

        RouletteManager.HighlightBettingSpace(new List<int>(){Number});
    }

    void onExit(BaseEventData e){
        if(RouletteManager.spinning){return;}

        RouletteManager.ClearHighlightedBettingSpaces();
    }

    void onClick(BaseEventData e){
        if(RouletteManager.spinning){return;}

        if(RouletteManager.MoneyAvailable < RouletteManager.SelectedChip){return;}


        GameObject newChip = Instantiate(RouletteManager.SelectedChipItem.gameObject, transform);
        newChip.GetComponent<RectTransform>().localPosition = Vector3.zero;
        newChip.GetComponent<RectTransform>().sizeDelta = new Vector2(50,50);

        RouletteManager.AddBet(RouletteManager.SelectedChip, CombinerType.Single, new List<int>(){Number}, newChip);
    }

    public void SetHighlighted(bool value){
        // image.color = value ? changedColor : defaultColor;
        GetComponent<Outline>().effectColor = new Color(1,1,1, (value) ? 0.5f:0);
    }
    
}
