using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace EDPlayerJournal; 
public class FactionState {
    public string? State { get; set; }
    public long? Trend { get; set; }

    public static FactionState? FromJSON(JObject j) {
        if (j == null) {
            return null;
        }

        FactionState s = new FactionState() {
            State = j.Value<string>("State"),
            Trend = j.Value<long?>("Trend"),
        };

        if (string.IsNullOrEmpty(s.State)) {
            return null;
        }

        return s;
    }
}

public class Faction {
    public string? Name { get; set; }
    public string? FactionState { get; set; }
    public string? Government { get; set; }
    public double Influence { get; set; }
    public string? Allegiance { get; set; }
    public string? Happiness { get; set; }
    public string? HappinessLocalised { get; set; }
    public double MyReputation { get; set; }
    public bool SquadronFaction { get; set; } = false;
    public bool HomeSystem { get; set; } = false;

    public List<FactionState> RecoveringStates { get; set; } = new List<FactionState>();
    public List<FactionState> ActiveStates { get; set; } = new List<FactionState>();
    public List<FactionState> PendingStates { get; set; } = new List<FactionState>();

    public static Faction? FromJSON(JObject j) {
        if (j == null) {
            return null;
        }

        Faction f = new Faction() {
            Name = j.Value<string>("Name") ?? "",
            FactionState = j.Value<string>("FactionState") ?? "",
            Government = j.Value<string>("Government") ?? "",
            Influence = j.Value<double?>("Influence") ?? 1.0,
            Allegiance = j.Value<string>("Allegiance") ?? "",
            Happiness = j.Value<string>("Happiness") ?? "",
            HappinessLocalised = j.Value<string>("Happiness_Localised") ?? "",
            MyReputation = j.Value<double?>("MyReputation") ?? 1.0,
            SquadronFaction = (j.Value<bool?>("SquadronFaction") ?? false),
            HomeSystem = (j.Value<bool?>("HomeSystem") ?? false),
        };

        if (string.IsNullOrEmpty(f.Name)) {
            return null;
        }

        JArray? recovering_states = j.Value<JArray>("RecoveringStates");
        if (recovering_states != null) {
            foreach (JObject s in recovering_states) {
                FactionState? state = EDPlayerJournal.FactionState.FromJSON(s);
                if (state != null) {
                    f.RecoveringStates.Add(state);
                }
            }
        }

        JArray? active_states = j.Value<JArray>("ActiveStates");
        if (active_states != null) {
            foreach (JObject s in active_states) {
                FactionState? state = EDPlayerJournal.FactionState.FromJSON(s);
                if (state != null) {
                    f.ActiveStates.Add(state);
                }
            }
        }

        JArray? pending_states = j.Value<JArray>("PendingStates");
        if (pending_states != null) {
            foreach (JObject s in pending_states) {
                FactionState? state = EDPlayerJournal.FactionState.FromJSON(s);
                if (state != null) {
                    f.PendingStates.Add(state);
                }
            }
        }

        return f;
    }
}