﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorldBankBBS.Logic;
using WorldBankBBS.Pathing;

namespace WorldBankBBS.FAQ
{
    public class FaqPathHandler : IPathHandler
    {
        public Session Session { get; }
        private FaqEvents _events = null;

        public FaqPathHandler(Session session)
        {
            Session = session;
            _events = BbsDispatcher.GetDispatcher(session).GetObject<FaqEvents>(typeof(FaqEvents).FullName);
            if (_events == null)
            {
                _events = new FaqEvents(session);
                BbsDispatcher.GetDispatcher(session).AddObject(typeof(FaqEvents).FullName, _events);
            }
        }

        public void ProcessPath(string path)
        {
            var parts = path.Split(new[] { '|' });

            switch (parts.FirstOrDefault()?.ToLower())
            {
                case "enterfaqsection":
                    _events.InvokeEnterFaqSection();
                    break;

                case "listfaqquestions":
                    _events.InvokeListQuestions();
                    break;

                case "addquestion":
                    _events.InvokeAddQuestion();
                    break;

                case "displayquestion":
                    int temp;
                    Guid guid;
                    if (parts.Length == 2 && Int32.TryParse(parts.Last(), out temp))
                    {
                        _events.InvokeDisplayQuestion(temp);
                    }
                    else if (parts.Length == 2 && Guid.TryParse(parts.Last(), out guid))
                    {
                        _events.InvokeDisplayQuestion(guid);
                    }
                    break;

                case "answerquestion":
                    int temp2;
                    if (parts.Length == 2 && Int32.TryParse(parts.Last(), out temp2))
                    {
                        _events.InvokeAnswerQuestion(temp2);
                    }
                    break;

            }
        }
    }
}
