using Plus.Communication.Packets.Outgoing.Rooms.Polls.Quick;
using Plus.HabboHotel.GameClients;
using Plus.HabboHotel.Rooms;
using Plus.HabboHotel.Rooms.Polls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Plus.Communication.Packets.Incoming.Rooms.Polls
{
    class AnswerPollQuestionEvent : IPacketEvent
    {
        public void Parse(GameClient Session, ClientPacket Packet)
        {
            int num1 = Packet.PopInt();
            int questId = Packet.PopInt();
            int num2 = Packet.PopInt();
            List<string> source = new List<string>();
            while (num2 > 0)
            {
                source.Add(Packet.PopString());
                checked { --num2; }
            }
            if (num1 == -2)
            {
                Room currentRoom = Session.GetHabbo().CurrentRoom;
                if (currentRoom == null || currentRoom.QuickPoll == null)
                    return;
                bool VoteYes = source.FirstOrDefault<string>() == "1";
                currentRoom.QuickPoll.AddVote(Session.GetHabbo().Id, VoteYes);
                foreach (RoomUser roomUser in currentRoom.GetRoomUserManager().GetUserList().Where(c => !c.IsBot))
                {
                    ConcludePollComposer concludePollComposer = new ConcludePollComposer(currentRoom.QuickPoll, Session.GetHabbo().Id, VoteYes ? 1 : 0);
                    currentRoom.SendMessage(concludePollComposer, false);
                }
            }
            else
            {
                if (RoomPollManager.GetPoll(num1) == null)
                    return;
                UserPollData userAnsweringPoll = RoomPollManager.GetUserAnsweringPoll(Session.GetHabbo().Id, num1);
                if (userAnsweringPoll == null)
                    return;
                userAnsweringPoll.Answer(questId, string.Join("\t", source.ToArray()));
                if (!userAnsweringPoll.CompletedAllQuestions)
                    return;
                RoomPollManager.FinishuserAnsweringPoll(userAnsweringPoll.UserId);
            }
        }
    }
}
