using System;
using System.Text;
using System.Globalization;
using EDPlayerJournal;
using System.Linq;

namespace EliteBGS.BGS;

public class NonaDiscordLog : DiscordLogGenerator {
    protected override string BotHeader() {
        var sb = new StringBuilder();
        sb.AppendFormat(":robot: `Bot-Header:` {0}; {1}\n", GetToolVersion(), this.Name);
        return sb.ToString();
    }

    private string FormatDate() {
        CultureInfo cultureInfo = CultureInfo.InvariantCulture;
        StringBuilder date = new StringBuilder();
        DateTime today = DateTime.UtcNow;
        string suffix;

        if (today.Day == 1 || today.Day == 21 || today.Day == 31) {
            suffix = "st";
        } else if (today.Day == 2 || today.Day == 22) {
            suffix = "nd";
        // Shakaka wins the price for finding this "bug"!
        } else if (today.Day == 3 || today.Day == 23) {
            suffix = "rd";
        } else {
            suffix = "th";
        }

        date.AppendFormat("{0} {1}{2}, {3}",
            cultureInfo.DateTimeFormat.GetMonthName(today.Month), 
            today.Day, suffix,
            today.Year + EliteDangerous.YearOffset
            );

        return date.ToString();
    }

    protected override string GenerateObjectiveHeader(Objective objective) {
        StringBuilder log = new StringBuilder();

        string location;

        if (!string.IsNullOrEmpty(objective.System) && !string.IsNullOrEmpty(objective.Faction)) {
            location = string.Format("{0}, {1}", objective.System, objective.Faction);
        } else if (!string.IsNullOrEmpty(objective.System)) {
            location = objective.System;
        } else {
            location = "Unknown Location";
        }

        int legacycount = objective.Transactions
                    .Where(x => x.IsLegacy)
                    .Count()
                    ;

        string summary = GenerateSummary(objective);

        log.AppendFormat(":globe_with_meridians: `Target:` {0}\n", location);
        if (!string.IsNullOrEmpty(summary)) {
            log.AppendFormat(":scroll: `Summary:` {0}\n", summary);
        }
        if (legacycount > 0) {
            log.Append(":rotating_light: `Warning`: Some actions were done in E:D Legacy\n");
        }
        log.Append(":clipboard: `Conducted:`\n");
        log.Append("```\n");

        return log.ToString();
    }

    protected override string GenerateObjectiveFooter(Objective objective) {
        return "```\n";
    }

    protected override string GenerateHeader() {
        return string.Format(":clock2: `Date:` {0}\n", FormatDate());
    }

    protected override string GenerateFooter() {
        return "\n";
    }

    public override string ToString() {
        return "Nova Navy";
    }

    public override string Name {
        get { return "NovaNavy"; }
    }

    public override string[] SplitLog(string log, int maxcount = 2000) {
        return SplitLogWithHeader(log, ":clock2: `Date:`", maxcount);
    }
}