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

// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
//    using AirPodsUI.Core.Models;
//
//    var devices = Devices.FromJson(jsonString);

namespace AirPodsUI.Core.Models
{
    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class DevicesJson
    {
        [JsonProperty("Devices")]
        public Device[] Devices { get; set; }
    }

    public partial class Device
    {
        [JsonProperty("Name")]
        public string Name { get; set; }

        [JsonProperty("Identifier")]
        public string Identifier { get; set; }

        [JsonProperty("DarkMode")]
        public bool DarkMode { get; set; }

        [JsonProperty("ToastType")]
        [JsonConverter(typeof(StringEnumConverter))]
        public Toast ToastType { get; set; }

        public static bool operator ==(Device a, Device b)
        {
            return a.Identifier == b.Identifier;
        }

        public static bool operator !=(Device a, Device b)
        {
            return a.Identifier != b.Identifier;
        }
    }

    public partial class DevicesJson
    {
        public static DevicesJson FromJson(string json) => JsonConvert.DeserializeObject<DevicesJson>(json, Converter.Settings);

        public static List<Device> GetDevices()
        {
            try
            {
                return DevicesJson.FromJson(File.ReadAllText(Common.DevicesFile)).Devices.ToList();
            }
            catch (Exception)
            {
                return new List<Device>();
            }
        }

        public static void SaveDevices(List<Device> input)
        {
            DevicesJson devs = new DevicesJson();
            devs.Devices = input.ToArray();
            File.WriteAllText(Common.DevicesFile, Serialize.ToJson(devs));
        }
    }

    public static class Serialize
    {
        public static string ToJson(this DevicesJson self) => JsonConvert.SerializeObject(self, Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
            Formatting = Formatting.Indented,
        };
    }

    public enum Toast
    {
        Pencil
    }
}
