using FogbugzWrapper;
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace FogbugzLogReader
{
    internal class Program
    {
        private const string OUTPUT_SEPERATOR = "========================";
        private const string SINGLE_TAB = "   ";

        private static void Main(string[] args)
        {
            var fogbugzClient = new FogbugzClient();
            if (string.IsNullOrEmpty(Settings1.Default.CurrentToken) && args.Length != 2)
            {
                Console.WriteLine("No Login Credentials!");
                Console.ReadKey();
                return;
            }
            else if (!string.IsNullOrEmpty(Settings1.Default.CurrentToken))
            {
                fogbugzClient = new FogbugzWrapper.FogbugzClient($@"http://{Properties.Settings.Default.ManuscriptServerAddress}/api.asp?", Settings1.Default.CurrentToken);
            }
            else
            {
                fogbugzClient = new FogbugzWrapper.FogbugzClient($@"http://{Properties.Settings.Default.ManuscriptServerAddress}/api.asp?", args[0], args[1]);
            }
            if (fogbugzClient == null)
            {
                Console.WriteLine($"Login failed for user:{args[0]}");
                return;
            }
            Settings1.Default.CurrentToken = fogbugzClient.Token;
            Settings1.Default.Save();
            var loggedin = fogbugzClient.IsLoggedIn();
            var keepEntering = true;
            while (keepEntering)
            {
                Console.Write(">");
                var input = Console.ReadLine();
                if (input == "q") { keepEntering = true; }
                else
                {
                    if (int.TryParse(input, out int inputInteger))
                    {
                        var allFBEvents = fogbugzClient.GetEvents(inputInteger).ToList();
                        if (allFBEvents.FirstOrDefault() == null)
                        {
                            Console.WriteLine($"ERROR: Ticket {input} is not valid.");
                        }
                        else
                        {
                            var sb = new StringBuilder();
                            sb.AppendLine($"{SINGLE_TAB}{fogbugzClient.GetStatus(inputInteger)}{Environment.NewLine}{SINGLE_TAB}[{fogbugzClient.GetProject(inputInteger)}] {fogbugzClient.GetTitle(inputInteger)} - {fogbugzClient.GetArea(inputInteger)}");
                            foreach (var fbEvent in allFBEvents)
                            {
                                sb.AppendLine($"{SINGLE_TAB}{fbEvent.TimeStamp} {fbEvent.User} ");
                                if (!string.IsNullOrEmpty(fbEvent.Comment))
                                {
                                    using (StringReader reader = new StringReader(fbEvent.Comment))
                                    {
                                        string line;
                                        sb.AppendLine($"{SINGLE_TAB}Comment:");
                                        while ((line = reader.ReadLine()) != null)
                                        {
                                            sb.AppendLine($"{SINGLE_TAB}{SINGLE_TAB}{line}");
                                        }
                                    }
                                }
                                if (!string.IsNullOrEmpty(fbEvent.ReleaseNote))
                                {
                                    using (StringReader reader = new StringReader(fbEvent.ReleaseNote))
                                    {
                                        string line;
                                        sb.AppendLine($"{SINGLE_TAB}Release Note:");
                                        while ((line = reader.ReadLine()) != null)
                                        {
                                            sb.AppendLine($"{SINGLE_TAB}{SINGLE_TAB}{line}");
                                        }
                                    }
                                }
                                if (0 != fbEvent.ZendeskNumber)
                                {
                                    using (StringReader reader = new StringReader(fbEvent.ZendeskNumber.ToString()))
                                    {
                                        string line;
                                        sb.AppendLine($"{SINGLE_TAB}Zendesk Number:");
                                        while ((line = reader.ReadLine()) != null)
                                        {
                                            sb.AppendLine($"{SINGLE_TAB}{SINGLE_TAB}{line}");
                                        }
                                    }
                                }
                                sb.AppendLine();
                            }
                            Console.WriteLine(sb.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("ERROR: Enter a valid ticket number");
                    }
                }
            }
        }
    }
}