using System.Collections.Generic;
using System;
using System.Text;
using Newtonsoft.Json;
using EDPlayerJournal.BGS;
using System.Linq;

namespace EliteBGS;

public class UITransaction {
    public bool IsEnabled { get; set; } = true;

    public bool IsExpanded { get; set; } = true;

    public Transaction Transaction { get; set; }

    public UITransaction() { }

    public UITransaction(Transaction transaction) {
        Transaction = transaction;
    }

    public string Name {
        get { return ToString(); }
    }

    public string CompletedAt {
        get { return Transaction.CompletedAt; }
    }

    public override string ToString() {
        return Transaction.ToString();
    }
}

public class Objective : IComparable<Objective> {
    public bool IsEnabled { get; set; }

    public List<UITransaction> UITransactions { get; } = new List<UITransaction>();

    public List<Transaction> Transactions {
        get { return UITransactions.Select(x => x.Transaction).ToList<Transaction>(); }
    }

    public string Name {
        get { return this.ToString(); }
    }

    public bool IsExpanded { get; set; }

    public void Clear() {
        if (Transactions == null) {
            return;
        }
        Transactions.Clear();
    }

    public bool Matches(string system, string faction) {
        return string.Compare(system, System) == 0 &&
               string.Compare(faction, Faction) == 0;
    }

    public int CompareTo(Objective other) {
        return (other.System == System &&
            other.Faction == Faction) ? 0 : -1;
    }

    public bool IsValid {
        get { return !string.IsNullOrEmpty(System) && !string.IsNullOrEmpty(Faction); }
    }

    public string System { get; set; }

    public string Faction { get; set; }

    public override string ToString() {
        StringBuilder str = new StringBuilder();
        if (!string.IsNullOrEmpty(System)) {
            str.AppendFormat("System: {0}", System);
        } else {
            str.AppendFormat("System: Unknown");
        }
        if (!string.IsNullOrEmpty(Faction)) {
            if (str.Length > 0) {
                str.Append(", ");
            }
            str.AppendFormat("Faction: {0}", Faction);
        }
        return str.ToString();
    }

    public List<Type> EnabledOfType<Type>() where Type : Transaction {
        return UITransactions
            .Where(x => x.IsEnabled)
            .Select(x => x.Transaction)
            .OfType<Type>()
            .ToList()
            ;
    }
}
