using System.Reactive.Linq;
using InnovEnergy.Lib.Victron.VeDBus;
// TODO THIS FILE IS LEGACY NOW?

namespace InnovEnergy.GuiFeeder;

using Services = IReadOnlyList<ServiceProperties>;

public static class Program{
    public static async Task Main()
    {
        //Set Values to be written into the file and the filepath of the gui data.json file
        const String filepath = @"/data/gui/data";
        
        
        //legacy
        // var trackedKeys = new String[]
        // {
        //     "Dc/Battery/Soc",                       //State of charge
        //     "Dc/Battery/Power",                     //Power of Battery
        //     "Dc/Vebus/Power",                       //Power on DC Bus
        //     "Ac/ConsumptionOnOutput/L1/Power",      //Consumption on Output aka CRITICAL CONSUMPTION
        //     "Ac/ConsumptionOnOutput/L2/Power",
        //     "Ac/ConsumptionOnOutput/L3/Power",
        //     "Ac/ConsumptionOnInput/L1/Power",       //Consumption on Input aka non-critical
        //     "Ac/ConsumptionOnInput/L2/Power",
        //     "Ac/ConsumptionOnInput/L3/Power",
        //     "Ac/Grid/L1/Power",                     //Power on Grid
        //     "Ac/Grid/L2/Power",
        //     "Ac/Grid/L3/Power",
        //     "Ac/PvOnOutput/L1/Power",               //Power on PV at Ac out bus
        //     "Ac/PvOnOutput/L2/Power",               
        //     "Ac/PvOnOutput/L3/Power",
        //     "Ac/PvOnGrid/L1/Power",                 //Power on PV at Ac in bus
        //     "Ac/PvOnGrid/L2/Power",
        //     "Ac/PvOnGrid/L3/Power",
        // };

        var trackedKeys = new String[]
        {
            "Voltage",
            "Current",
            "Soc",
            "Temperature"
        };

        //How often should we sample
        const Int32 samplePeriodSecs = 1;
        var interval = TimeSpan.FromSeconds(samplePeriodSecs);

        //legacy
        // //Setup dbus connection and read the data.json file
        // var dbus = new DBusConnection(Bus.System);

        //legacy
        //Services to track
        // var names = new[]
        //     { "com.victronenergy.system" };
        
        Console.WriteLine("start sampling");
        // Observable
        //     .Interval(interval)
        //     .Subscribe(async (b) => await UpdateDataFile(names, dbus, trackedKeys, filepath));
        
        while (true)
        {
            await UpdateDataFile(trackedKeys, filepath);
            Thread.Sleep(interval);
        }
    }

    private static async Task UpdateDataFile(String[] trackedKeys, String filepath)
    {
        //We use time as timestamp and as a tracker of how many entries we allow
        var time = DateTimeOffset.Now.ToUnixTimeSeconds();
        var timemod = (time % 180).ToString();
        var dict = new Dictionary<String, Object>();

        foreach (var name in names)
        {
            var values = await dbus.GetAllValues(name);
            //Write the tracked Values into JSON
            foreach (var key in values.Keys)
            {
                // Console.WriteLine(!trackedKeys.Contains(key));
                //we only take values we want to display
                if (!trackedKeys.Contains(key)) continue;
                dict.Add(key, values[key]);
            }
            //TODO Implement Time dependence (here and in py)
        }

        //Write File after each change in Values
        var jsonObject = JsonConvert.SerializeObject(dict);
        File.WriteAllText(filepath + timemod + ".json", jsonObject);
        Console.WriteLine("Updated " + filepath + timemod + ".json");
    }
}