using EliteBGS.LogGenerator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;

namespace EliteBGS;

public class OneLineDiscordLog : DiscordLogGenerator {
    protected override string GenerateObjectiveHeader(Objective objective) {
        if (!string.IsNullOrEmpty(objective.Faction)) {
            return string.Format("**{0}** for **{1}**: ", objective.System, objective.Faction);
        } else {
            return string.Format("**{0}**: ", objective.System);
        }
    }

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

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

    protected override string GenerateHeader() {
        return "";
    }

    public override string[] SplitLog(string log, int maxcount = 2000) {
        string[] lines = log.Split('\n');
        List<string> chunks = new();
        string chunk = string.Empty;

        // Optimisation
        if (log.Length <= maxcount) {
            return new string[] { log };
        }

        for (int i = 0; i < lines.Length; i++) {
            string line = lines[i];
            if ((chunk.Length + line.Length) > maxcount || i == lines.Length - 1) {
                chunks.Add(chunk.Trim());
                chunk = string.Empty;
                chunk = chunk.Insert(0, BotHeader()).Trim();
            } else {
                chunk = chunk + "\n" + line;
            }
        }

        return chunks.ToArray();
    }

    public override string GenerateDiscordLog(Report report) {
        StringBuilder log = new StringBuilder();

        if (report == null) {
            return "";
        }

        var objectives = report.Objectives
            .Where(x => x.IsEnabled && x.Transactions.Count() > 0)
            ;

        if (objectives == null || objectives.Count() <= 0) {
            return "";
        }

        log.AppendFormat("{0}", BotHeader());

        foreach (Objective objective in objectives) {
            log.AppendFormat("{0}", GenerateObjectiveHeader(objective));

            foreach (LogFormatter formatter in formatters) {
                string text = formatter.GenerateSummary(objective);
                text = text.Trim();
                if (!string.IsNullOrEmpty(text)) {
                    log.AppendFormat("{0}; ", text);
                }
            }

            log.AppendFormat("{0}", GenerateObjectiveFooter(objective));
        }

        log.AppendFormat("{0}", GenerateFooter());

        return log.ToString().Trim();
    }

    public override string ToString() {
        return "One Line";
    }

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