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.Outgoing.Rooms.Polls
{
    class PollQuestionsComposer : ServerPacket
    {
        public PollQuestionsComposer(Poll Poll)
          : base(ServerPacketHeader.PollQuestionsMessageComposer)
        {
            this.WriteInteger(Poll.Id);
            this.WriteString(Poll.Title);
            this.WriteString(Poll.Greeting);
            this.WriteInteger(Poll.Questions.Count);
            foreach (PollQuestion question in Poll.Questions)
            {
                this.WriteInteger(question.Id);
                this.WriteInteger(question.Id);
                this.WriteInteger((int)question.Type);
                this.WriteString(question.Question);
                this.WriteInteger(question.Id);
                this.WriteInteger(-1);
                this.WriteInteger(question.Type < PollQuestionType.TEXTBOX ? question.Answers.Count : 0);
                if (question.Type < PollQuestionType.TEXTBOX)
                {
                    foreach (PollAnswerItem answer in question.Answers)
                    {
                        this.WriteString(answer.Id.ToString());
                        this.WriteString(answer.AnswerText);
                        this.WriteInteger(answer.TargetSubQuestionId);
                    }
                }
                this.WriteInteger(question.SubQuestions.Count);
                foreach (PollQuestion subQuestion in question.SubQuestions)
                {
                    this.WriteInteger(subQuestion.Id);
                    this.WriteInteger(subQuestion.Id);
                    this.WriteInteger((int)subQuestion.Type);
                    this.WriteString(subQuestion.Question);
                    this.WriteInteger(subQuestion.Id);
                    this.WriteInteger(-1);
                    this.WriteInteger(subQuestion.Type < PollQuestionType.TEXTBOX ? subQuestion.Answers.Count : 0);
                    if (subQuestion.Type < PollQuestionType.TEXTBOX)
                    {
                        foreach (PollAnswerItem answer in subQuestion.Answers)
                        {
                            this.WriteString(answer.Id.ToString());
                            this.WriteString(answer.AnswerText);
                            this.WriteInteger(answer.TargetSubQuestionId);
                        }
                    }
                }
            }
            this.WriteBoolean(Poll.SomeBool);
        }

        public void WriteQuestion(PollQuestion Quest)
        {
            this.WriteInteger(Quest.Id);
            this.WriteInteger(Quest.Id);
            this.WriteInteger((int)Quest.Type);
            this.WriteString(Quest.Question);
            this.WriteInteger(0);
            this.WriteInteger(0);
            this.WriteInteger(Quest.Type < PollQuestionType.TEXTBOX ? Quest.Answers.Count : 0);
            if (Quest.Type >= PollQuestionType.TEXTBOX)
                return;
            foreach (PollAnswerItem answer in Quest.Answers)
            {
                this.WriteString(answer.Id.ToString());
                this.WriteString(answer.AnswerText);
                this.WriteInteger(answer.TargetSubQuestionId);
            }
        }
    }
}
