﻿using Rinora.Authentication;
using Rinora.Backend.Objects.Accounts;
using Rinora.Backend.Services;
using Rinora.Text;
using System.Linq;

namespace Rinora.Accounts
{
    public class AccountOutput : IAccountOutput
    {
        private readonly IAccountService accountService;

        public AccountOutput(IAccountService accountService)
        {
            this.accountService = accountService;
        }

        public void CheckMessages(User user)
        {
            int msgCount = user.Account.Messages.Count(x => !x.Read.HasValue);
            if (msgCount == 0)
            {
                user.Send("You have no new messages.\r\n");
            }
            else
            {
                string messageStr = msgCount > 1 ? "messages" : "message";

                user.Send(
                    new Segment(Colors.White, "You have "),
                    new Segment(Colors.Red, msgCount.ToString()),
                    new Segment(Colors.White, $" new {messageStr}.\r\n")
                );
            }
        }

        public void CreateCharacter(User user)
        {
            user.Send(
                new Segment(Colors.White, "Type '"),
                new Segment(Colors.Red, "create"),
                new Segment(Colors.White, "' to create a new character.\r\n")
            );
        }

        public void ListCharacters(User user)
        {
            user.Send("Select a character:\r\n");

            int i = 1;

            foreach (Character character in user.Account.Characters)
            {
                user.Send(
                    new Segment("    "),
                    new Segment(Colors.Red, $"{i}."),
                    new Segment(Colors.White, $" {character.Name}\r\n")
                );
                i++;
            }

            user.Send("\r\n");
        }

        public void ShowLegacy(User user, int count)
        {
            user.Send(
                new Segment("You have "),
                new Segment(Colors.White, count.ToString()),
                new Segment(" characters whose legacy you haven't passed on.\r\n")
            );
        }

        public void ShowMessage(User user)
        {
            user.Send(
                new Segment(Colors.White, "Type '"),
                new Segment(Colors.Red, "messages"),
                new Segment(Colors.White, "' to view your messages.\r\n")
            );
        }

        public void ShowAccountScreen(User user)
        {
            CheckMessages(user);
            user.Send("\r\n");

            user.ActiveCharacters = user.Account.GetCurrentCharacters();

            if (user.ActiveCharacters.Count > 0)
            {
                ListCharacters(user);
            }

            if (user.ActiveCharacters.Count == 0)
            {
                user.Send(new Segment(Colors.Yellow, "You have no characters!\r\n"));
            }

            if (user.ActiveCharacters.Count < 5)
            {
                CreateCharacter(user);
            }

            if (user.Account.Messages.Any())
                ShowMessage(user);

            int legacyCount = user.Account.GetLegacyCharacters().Count;

            if (legacyCount > 0)
                ShowLegacy(user, legacyCount);
        }
    }

    public interface IAccountOutput
    {
        /// <summary>
        /// Sends the number of messages pending for the user
        /// </summary>
        /// <param name="user"></param>
        void CheckMessages(User user);

        /// <summary>
        /// Shows the text to create a new character
        /// </summary>
        /// <param name="user"></param>
        void CreateCharacter(User user);

        /// <summary>
        /// Prompts the user on their current characters
        /// </summary>
        /// <param name="user"></param>
        void ListCharacters(User user);

        /// <summary>
        /// Tells the user how many legacy characters they have
        /// </summary>
        /// <param name="user"></param>
        /// <param name="count"></param>
        void ShowLegacy(User user, int count);

        /// <summary>
        /// Tells the user how to view their messages
        /// </summary>
        /// <param name="user"></param>
        void ShowMessage(User user);

        /// <summary>
        /// Shows the main account screen for the user
        /// </summary>
        /// <param name="user"></param>
        void ShowAccountScreen(User user);
    }
}
