using DSharpPlus.Entities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace IFE_0._3
{
    abstract internal class Vote
    {
        
        //Bro, here we're just gonna treat the ints as string so we can generalize this
        //Todo: Use Templates instead of this^
        //The key is what you're voting for, the int is how many times it's been voted
        protected Dictionary<string, int> voteData = new Dictionary<string, int>();

        //A list of the Votees with a bool attached as to weather or not they've voted yet
        protected Dictionary<Player, bool> votees;
        
        //The interactable Buttons to send to players
        List<DiscordComponent> buttons = new List<DiscordComponent>();
        int voteCount = 0;
        bool finished = false;
        //Message to send at the start of the vote
        string gameChannelStartMessage = "";
        protected Messanger messanger;

        //Constructor
        public Vote(List<Player> players, DiscordChannel gChannel, Messanger msgr)
        {
            votees = new Dictionary<Player, bool>();
            foreach(Player player in players)
            {
                votees.Add(player, false);
            }

            messanger = msgr;
            new DiscordMessageBuilder().WithContent(gameChannelStartMessage);
            populateButtons();
            sendButtons();
        }

        //Returns weather a vote has been cast or not
        public bool testVote(Player player) 
        {
            return votees[player];
        }

        //These two functions just create and send the Buttons to players
        public abstract void sendButtons();
        public abstract void populateButtons();

        //Casts vote 
        public void castVote(string key, Player player) {
            if (voteData.TryGetValue(key, out voteCount))
            {
                voteData[key]++;
            }
            else
            {
                voteData.Add(key, 1);
            }
            if (voteOver())
            {
                voteFinish();
            }
        }

        //Checks weather or not the all players have voted
        bool voteOver()
        {
            for (int x = 0; x < votees.Count; x++)
            {

            }

            bool voteOverFlag = true;
            foreach(bool voted in votees.Values)
            {
                if (!voted)
                {
                    voteOverFlag = false;
                }
            }

            return voteOverFlag; 
        }

        //In the case of a tie, the vote will usually defer to captain
        protected virtual string breakTie(Player captain)
        {
            return captain.lastVote.ToString();
        }

        //What happens when the finishes and what to do with the result
        public abstract void voteFinish();
    }
}