using System.Collections.Generic;
using System;
using System.Text;
using Newtonsoft.Json;

namespace EDPlayerJournal.BGS;

public class Objective : IComparable<Objective> {
    private List<Transaction> entries = new List<Transaction>();

    [JsonIgnore]
    public bool IsEnabled { get; set; }

    [JsonIgnore]
    public List<Transaction> Children {
        get => entries;
    }

    [JsonIgnore]
    public string Name => this.ToString();

    [JsonIgnore]
    public bool IsExpanded { get; set; }

    [JsonIgnore]
    public List<Transaction> LogEntries {
        get => entries;
        set => entries = value;
    }

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

    public int Matches(Transaction e) {
        int match_count = 0;

        if (e.OnlyControllingFaction) {
            if (Faction == null || (e.Faction != Faction)) {
                return 0;
            }
        }

        if (e.Faction != null && Faction != null) {
            if (string.Compare(e.Faction, Faction, true) != 0) {
                /* if we have a faction, and it doesn't match we don't care.
                 * faction is the most important comparision, so if it doesn't match
                 * it is not the right objective
                 */
                return 0;
            } else {
                ++match_count;
            }
        }

        /* system and station only add to the match strength though */
        if (e.System != null && System != null) {
            if (string.Compare(e.System, System, true) == 0) {
                ++match_count;
            }
        }

        /* if system and faction already match, station is not so important */
        if (e.Station != null && Station != null) {
            if (string.Compare(e.Station, Station, true) == 0) {
                ++match_count;
            }
        }

        return match_count;
    }

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

    public bool IsValid => (!string.IsNullOrEmpty(System) && !string.IsNullOrEmpty(Faction));

    public string System { get; set; } = "";

    public string Station { get; set; } = "";

    public string Faction { get; set; } = "";

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

    public string ToShortString() {
        StringBuilder str = new StringBuilder();
        if (System != null && System.Length > 0) {
            str.AppendFormat("{0}", System);
        }
        if (Station != null && Station.Length > 0) {
            if (str.Length > 0) {
                str.Append(", ");
            }
            str.AppendFormat("{0}", Station);
        }
        return str.ToString();
    }
}
