// Decompiled with JetBrains decompiler
// Type: FiestaLauncher.Services.SettingsService
// Assembly: FiestaOnline, Version=2019.5.22.0, Culture=neutral, PublicKeyToken=null
// MVID: 2FD5EB61-BC25-469D-997E-88998E849F6A
// Assembly location: C:\Users\marbo\Documents\Gamigo 2020 Client\FiestaOnline.exe

using FiestaLauncher.gClasses;
using System;
using System.Collections.Generic;
using System.Xml;

namespace FiestaLauncher.Services
{
  public class SettingsService
  {
    private Dictionary<string, string> Sets = new Dictionary<string, string>();
    internal Dictionary<int, List<WebFrame>> WebFrames = new Dictionary<int, List<WebFrame>>();
    internal List<ExternalFile> Externals = new List<ExternalFile>();
    internal List<Notice> Notices = new List<Notice>();
    private LauncherUpdater launcherUpdater;

    public string this[string index]
    {
      get
      {
        return this.Sets.ContainsKey(index) ? this.Sets[index] : "";
      }
      set
      {
        if (this.Sets.ContainsKey(index))
          this.Sets[index] = value;
        else
          this.Sets.Add(index, value);
      }
    }

    internal void Reset(string xml)
    {
      XmlDocument xmlDocument = new XmlDocument();
      xmlDocument.LoadXml(xml);
      this["Maint"] = xmlDocument.SelectNodes("FiestaLauncher")[0].Attributes["maint"].Value;
      this.Notices.Clear();
      foreach (XmlNode selectNode in xmlDocument.SelectNodes("FiestaLauncher/Notices/Notice"))
        this.Notices.Add(new Notice()
        {
          Enabled = int.Parse(selectNode.Attributes["enabled"].Value),
          Type = int.Parse(selectNode.Attributes["type"].Value),
          AutoShow = int.Parse(selectNode.Attributes["autoshow"].Value),
          Lifetime = int.Parse(selectNode.Attributes["lifetime"].Value),
          Title = selectNode.SelectNodes("NoticeTitle")[0].InnerText,
          Text = selectNode.SelectNodes("NoticeText")[0].InnerText
        });
      gFiestaLauncher.MainWindow.RunOnUiThread((Main.RunOnUiThreadDelegate) (() => gFiestaLauncher.MainWindow.ReloadSettingsComplete()));
    }

    public void Set(string xml)
    {
      try
      {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(xml);
        this["Maint"] = xmlDocument.SelectNodes("FiestaLauncher")[0].Attributes["maint"].Value;
        this["PatchHive"] = xmlDocument.SelectNodes("FiestaLauncher")[0].Attributes["patchhive"].Value;
        this["Startup"] = xmlDocument.SelectNodes("FiestaLauncher/FiestaClientStartup")[0].InnerText;
        this["StartupBin"] = xmlDocument.SelectNodes("FiestaLauncher/FiestaClientStartup")[0].Attributes["name"].Value;
        this["swf.PNG"] = xmlDocument.SelectNodes("FiestaLauncher/Background")[0].Attributes["png"].Value;
        LangService.Set(xmlDocument.SelectNodes("FiestaLauncher")[0].Attributes["lang"].Value);
        foreach (XmlNode selectNode in xmlDocument.SelectNodes("FiestaLauncher/Frames/Frame"))
        {
          WebFrame webFrame = new WebFrame()
          {
            Page = int.Parse(selectNode.Attributes["page"].Value),
            X = int.Parse(selectNode.Attributes["x"].Value),
            Y = int.Parse(selectNode.Attributes["y"].Value),
            W = int.Parse(selectNode.Attributes["w"].Value),
            H = int.Parse(selectNode.Attributes["h"].Value),
            Src = selectNode.Attributes["src"].Value
          };
          if (!this.WebFrames.ContainsKey(webFrame.Page))
            this.WebFrames.Add(webFrame.Page, new List<WebFrame>());
          this.WebFrames[webFrame.Page].Add(webFrame);
        }
        foreach (XmlNode selectNode in xmlDocument.SelectNodes("FiestaLauncher/Buttons/Button"))
          this["swf." + selectNode.Attributes["id"].Value] = selectNode.Attributes["src"].Value;
        foreach (XmlNode selectNode in xmlDocument.SelectNodes("FiestaLauncher/Externals/External"))
          this.Externals.Add(new ExternalFile()
          {
            Name = selectNode.Attributes["name"].Value,
            Src = selectNode.Attributes["src"].Value,
            Hash = selectNode.Attributes["hash"].Value
          });
        foreach (XmlNode selectNode in xmlDocument.SelectNodes("FiestaLauncher/Notices/Notice"))
        {
          if (int.Parse(selectNode.Attributes["enabled"].Value) == 1)
            this.Notices.Add(new Notice()
            {
              Enabled = int.Parse(selectNode.Attributes["enabled"].Value),
              Type = int.Parse(selectNode.Attributes["type"].Value),
              AutoShow = int.Parse(selectNode.Attributes["autoshow"].Value),
              Lifetime = int.Parse(selectNode.Attributes["lifetime"].Value),
              Title = selectNode.SelectNodes("NoticeTitle")[0].InnerText,
              Text = selectNode.SelectNodes("NoticeText")[0].InnerText
            });
        }
        foreach (XmlNode selectNode in xmlDocument.SelectNodes("FiestaLauncher/EmergencyUpdate"))
        {
          if (!(selectNode.Attributes["url"].ToString() == ""))
          {
            this.launcherUpdater = new LauncherUpdater(true, selectNode.Attributes["url"].ToString());
            gFiestaLauncher.SplashWindow.Hide();
          }
        }
      }
      catch (Exception ex)
      {
        gFiestaLauncher.onCrash(ex);
      }
    }
  }
}
