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

public class BettingSpaceCombiner : MonoBehaviour
{
    public CombinerType combinerType;
    public List<int> combinedNumbers;

    EventTrigger eventTrigger;

    void Start(){
        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);
    }

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

        RouletteManager.HighlightBettingSpace(combinedNumbers);
        // Debug.Log("Comb : " + combinedNumbers.ToString());
    }

    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.parent.parent);
        newChip.GetComponent<RectTransform>().position = GetComponent<RectTransform>().position;
        newChip.GetComponent<RectTransform>().sizeDelta = new Vector2(50,50);

        RouletteManager.AddBet(RouletteManager.SelectedChip, combinerType, combinedNumbers, newChip);
    }

    void OnValidate(){
        string[] args = name.Split("_");
        if(args[0] == "Split"){
            combinerType = CombinerType.Split;
        }else if(args[0]== "Street"){
            combinerType = CombinerType.Street;
        }else if(args[0] == "Corner"){
            combinerType = CombinerType.Corner;
        }else if(args[0] == "Five"){
            combinerType = CombinerType.Five;
        }else if(args[0] == "Line"){
            combinerType = CombinerType.Corner;
        }else if(args[0] == "Dozen"){
            combinerType = CombinerType.Dozen;
            combinedNumbers = new List<int>();
            int offset = 1;
            if(args[1] == "2"){
                offset+=12;
            }else if(args[1] == "3"){
                offset += 24;
            }
            for(int i=0; i < 12; i++){
                combinedNumbers.Add(i+ offset);
            }
            return;
        }else if(args[0] == "Row"){
            combinerType = CombinerType.Row;
            combinedNumbers = new List<int>();
            int mult = 0;
            if(args[1] == "2"){
                mult=1;
            }else if(args[1] == "3"){
                mult=2;
            }
            for(int i=0; i < 36; i+=3){
                combinedNumbers.Add((i+1) + mult);
            }
            return;
        }else if(args[0] == "18"){
            combinerType = CombinerType.Eighteen;
            combinedNumbers = new List<int>();
            int offset = 1;
            if(args[1] == "2"){
                offset += 18;
            }
            for(int i=0; i < 18; i++){
                combinedNumbers.Add((i+1) + offset);
            }
            return;
        }else if(args[0] == "Odd"){
            combinerType = CombinerType.Odd;
            combinedNumbers = new List<int>();
            for(int i=0; i < 36; i++){
                if(i%2!=0){combinedNumbers.Add((i+1));}
            }
            return;
        }else if(args[0] == "Even"){
            combinerType = CombinerType.Even;
            combinedNumbers = new List<int>();
            for(int i=0; i < 36; i++){
                if(i%2==0){combinedNumbers.Add((i+1));}
            }
            return;
        }else if(args[0] == "Red"){
            combinerType = CombinerType.Red;
            combinedNumbers = new List<int>();
            for(int i=0; i < 36; i++){
                if(RouletteManager.Reds.Contains((i+1))){combinedNumbers.Add((i+1));}
            }
            return;
        }else if(args[0] == "Black"){
            combinerType = CombinerType.Black;
            combinedNumbers = new List<int>();
            for(int i=0; i < 36; i++){
                if(!RouletteManager.Reds.Contains((i+1))){combinedNumbers.Add((i+1));}
            }
            return;
        }{
            return;
        }

        combinedNumbers = new List<int>();
        for(int i=1; i < args.Length; i++){
            combinedNumbers.Add(int.Parse(args[i]));
        }

        // GetComponent<Image>().color = new Color(1,1,1,0);
    }
}


public enum CombinerType{
    Split,
    Street,
    Corner,
    Line,
    Five,
    Dozen,
    Row,
    Eighteen,
    Red,
    Black,
    Odd,
    Even,
    Single
}
