/* This file is part of GPump, a Pump.io client.
 *
 * GPump (THE SOFTWARE) is made available under the terms and conditions of the
 * GNU Lesser General Public Licence 3.0. A copy of the licence can be read
 * in the file lgpl-3.0.txt in the root of this project.
 */

const Gtk = imports.gi.Gtk;
const _ = imports.gettext.gettext;
const Lang = imports.lang;

const SettingsData = imports.settings_data;

/** @class
 * PreferencesUI.
 */
const PreferencesUI = Lang.Class ({
  Name: "PrefrencesUI",
  Extends: Gtk.Dialog,
  
  _init: function (params) {
    this.parent ({
      use_header_bar: true,
      modal: true,
      title: _("GPump preferences"),
      width_request: 550,
      height_request: 300
    });
    
    if (params != undefined) {
      for (let k in params) {
        this[k] = params[k];
      }
    }
    
    this._prepare_header_bar ();
    this._prepare_list_box ();
    
    this.set_property ('resizable', false);
    
    this.show_all ();
  },
  
  _prepare_header_bar: function () {
    
    this.headerbar = this.get_header_bar ();
    this.headerbar.set_show_close_button (false);
    this.headerbar.pack_end ((this.close_btn = new Gtk.Button ({
      label: _("close")
    })));
    
    /* Install a custom style */
    let special_btn_style_ctx = this.close_btn.get_style_context ();
    special_btn_style_ctx.add_class ("suggested-action");
    
    this.close_btn.connect ('clicked', Lang.bind (this, function () {
      SettingsData.get_settings ().commit_to_file ();
      this.destroy ();
    }));
    
  },
  
  _prepare_list_box: function () {
    /*this.get_content_area ().pack_start ()),
                                         true, true, 25);*/
    this.get_content_area ().set_center_widget (
      (this.layout = new Gtk.Box ({
        orientation: Gtk.Orientation.VERTICAL,
        spacing: 8,
        border_width: 8,
        margin: 50
      }))
    );
    
    
    this.layout.pack_start (
      new Gtk.Frame ({child:
        (this.list_box = new Gtk.ListBox ({
          width_request: 400,
          selection_mode: Gtk.SelectionMode.NONE,
          expand: false
        })),
        expand: false
      }),
      true,
      true,
      null
    );
    
    let settings_object = SettingsData.get_settings ();
    
    /*************************** Use dark setting *****************************/
    
    this.use_dark_hbox = new Gtk.Box ({orientation: Gtk.Orientation.HORIZONTAL,
                                       spacing: 5});
    
    this.list_box.add (this.use_dark_hbox);
    
    this.use_dark_hbox.pack_start ((new Gtk.Label ({
      label: _("Use dark theme")
    })),false, false, 3);
    
    this.use_dark_hbox.pack_end ((this.use_dark_reset = new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: "edit-clear"}))
    })),false, false, 3);
    
    this.use_dark_hbox.pack_end ((this.use_dark_switch = new Gtk.Switch ()),
                                 false, false, 3);
    
    this.use_dark_switch.set_active (
      settings_object.get_setting("main.use_dark").data
    );
    
    this.use_dark_switch.connect("notify::active",
                                                  Lang.bind (this, function () {
      let settings = Gtk.Settings.get_default ();
      let app_settings = SettingsData.get_settings ();
      if ( this.use_dark_switch.get_active () ) {
        settings["gtk_application_prefer_dark_theme"] = true;
        app_settings.set_setting ("main.use_dark", true);
      } else {
        settings["gtk_application_prefer_dark_theme"] = false;
        app_settings.set_setting ("main.use_dark", false);
      }
    }));
    
    this.use_dark_reset.connect ("clicked", Lang.bind (this,
                                 this._reset_use_dark_theme));
    
    /*********************** Quit on close ************************************/
    this.quit_on_close_hbox = new Gtk.Box ({
      orientation: Gtk.Orientation.HORIZONTAL,
      spacing: 5
    });
    
    this.list_box.add (this.quit_on_close_hbox);
    
    this.quit_on_close_hbox.pack_start ((new Gtk.Label ({
      label: _("Quit on close")
    })), false, false, 3);
    
    this.quit_on_close_hbox.pack_end ((this.quit_on_close_reset =
                                       new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: "edit-clear"}))
    })), false, false, 3);
    
    this.quit_on_close_hbox.pack_end ((this.quit_on_close_switch =
                                       new Gtk.Switch), false, false, 3);
    
    if (settings_object.get_setting ("main.quit_on_close").data !=
                                       SettingsData.QUIT_ON_CLOSE.UNKNOWN) {
      this.quit_on_close_switch.set_active (Boolean (
        settings_object.get_setting ("main.quit_on_close")));
      print ("Herro!")
    } else {
      this.quit_on_close_switch.set_active (false);
    }
    
    this.quit_on_close_switch.connect ("notify::active",
                                       Lang.bind (this, function () {
      let v = settings_object.get_setting ("main.quit_on_close").data;
      
      switch (v) {
        case (SettingsData.QUIT_ON_CLOSE.YES):
        case (SettingsData.QUIT_ON_CLOSE.NO):
          if (this.quit_on_close_switch.get_active ()) {
            settings_object.set_setting ("main.quit_on_close", SettingsData.QUIT_ON_CLOSE.YES);
          } else {
            settings_object.set_setting ("main.quit_on_close", SettingsData.QUIT_ON_CLOSE.NO);
          }
          break;
          break;
        case (SettingsData.QUIT_ON_CLOSE.UNKNOWN):
          let popover = new Gtk.Popover ({relative_to: this.quit_on_close_switch, modal: true});
          let popover_layout1 = new Gtk.Box ({parent: popover, orientation: Gtk.Orientation.VERTICAL});
          popover_layout1.pack_start ((new Gtk.Label ({
            label: _("This has not been set yet, if you like to set it to a " +
            "value, click \"yes\" or \"no\", dismiss this to not set a value"),
            max_width_chars: 25,
            wrap: true
          })),false,false,0);
          let popover_layout2 = new Gtk.Box ({orientation: Gtk.Orientation.HORIZONTAL});
          popover_layout2.get_style_context().add_class ("linked");
          let btn_yes = new Gtk.Button ({label: _("Yes")});
          let btn_no = new Gtk.Button ({label: _("No")});
          popover_layout2.pack_end (btn_yes, true, true, 0);
          popover_layout2.pack_start (btn_no, true, true, 0);
          popover_layout1.pack_end (popover_layout2, false, false, 0);
          btn_no.get_style_context ().add_class("suggested-action");
          
          popover.connect_after ("hide", Lang.bind (popover, function () {
            popover.destroy ();
          }));
          
          btn_no.connect ("clicked", Lang.bind ((popover, this), function () {
            SettingsData.get_settings ().set_setting ("main.quit_on_close",
            SettingsData.QUIT_ON_CLOSE.NO);
            this.quit_on_close_switch.set_active (false);
            popover.hide ();
          }));
          
          btn_yes.connect ("clicked", Lang.bind ((popover, this), function () {
            SettingsData.get_settings ().set_setting ("main.quit_on_close",
            SettingsData.QUIT_ON_CLOSE.YES);
            this.quit_on_close_switch.set_active (true);
            popover.hide ();
          }));
          
          popover.show_all ();
          break;
        default:
          print ("WOW! Something went wrong!");
          print (JSON.stringify(v).toString ());
      }
    }));
    
    
    /*************************** Privacy settings *****************************/
    this.list_box.add ((new Gtk.Label ({label: _("Privacy settings")})));
    
    /************************ show full name setting **************************/
    
    this.show_full_name_hbox = new Gtk.Box ({
      orientation: Gtk.Orientation.HORIZONTAL,
      spacing: 5
    });
    
    this.list_box.add (this.show_full_name_hbox);
    
    this.show_full_name_hbox.pack_start ((new Gtk.Label ({
      label: _("Show full name")
    })),false, false, 3);
    
    this.show_full_name_hbox.pack_end ((this.show_full_name_reset =
      new Gtk.Button ({
        image: (new Gtk.Image ({icon_name: "edit-clear"}))
      })), false, false, 3);
    
    this.show_full_name_hbox.pack_end (
      (this.show_full_name_switch = new Gtk.Switch ()), false, false, 3);
    
    this.show_full_name_switch.set_active (
      settings_object.get_setting ("main.privacy.show_full_name").data
    );
    
    this.show_full_name_switch.connect ("notify::active",
                                        Lang.bind (this, function () {
      let app_settings = SettingsData.get_settings ();
      if (this.show_full_name_switch.get_active ()) {
        app_settings.set_setting ("main.privacy.show_full_name", true);
      } else {
        app_settings.set_setting ("main.privacy.show_full_name", false);
      }
    }));
    
    this.show_full_name_reset.connect ("clicked", Lang.bind (this,
                                                  this._reset_show_full_name));
    
    /*********************** only show avatar setting **************************/
    
    this.only_show_avatar_hbox = new Gtk.Box ({orientation: Gtk.Orientation.HORIZONTAL,
                                       spacing: 5});
    
    this.list_box.add (this.only_show_avatar_hbox);
    
    this.only_show_avatar_hbox.pack_start ((new Gtk.Label ({
      label: _("Only show avatar")
    })), false, false, 3);
    
    this.only_show_avatar_hbox.pack_end ((this.only_show_avatar_reset = 
                                          new Gtk.Button ({
      image: (new Gtk.Image ({icon_name: "edit-clear"}))
    })), false, false, 3);
    
    this.only_show_avatar_hbox.pack_end (
                             (this.only_show_avatar_switch = new Gtk.Switch ()),
                                 false, false, 3);
    this.only_show_avatar_switch.set_active (
      settings_object.get_setting ("main.privacy.only_show_avatar").data
    );
    
    this.only_show_avatar_switch.connect ("notify::active",
                                          Lang.bind (this, function () {
      let app_settings = SettingsData.get_settings ();
      if (this.only_show_avatar_switch.get_active ()) {
        app_settings.set_setting ("main.privacy.only_show_avatar", true);
      } else {
        app_settings.set_setting ("main.privacy.only_show_avatar", false);
      }
    }));
    
    /* Reset all settings */
    this.layout.pack_end (
      (this.reset_all_btn = new Gtk.Button ({
        label: _("Reset all settings")
      })),
      false,
      false,
      null
    );
    
    this.reset_all_btn.connect ("clicked", Lang.bind(this, this._reset_all));
  },
  
  _reset_all: function () {
    this._reset_use_dark_theme ();
    this._reset_show_full_name ();
    this._reset_only_show_avatar ();
    this._reset_quit_on_close
  },
  
  _reset_use_dark_theme: function () {
    /* If the switch is active */
    if ((this.use_dark_switch.get_active ())) {
      this.use_dark_switch.set_active (false);
    }
  },
  
  _reset_show_full_name: function () {
    if (!(this.show_full_name_switch.get_active ())) {
      this.show_full_name_switch.set_active (true);
    }
  },
  
  _reset_only_show_avatar: function () {
    if ((this.only_show_avatar_switch.get_active ())) {
      this.only_show_avatar_switch.set_active (false);
    }
  },
  
  _reset_quit_on_close: function () {
    if ((this.quit_on_close_switch.get_active())) {
      this.quit_on_close_switch.set_active (false);
    }
  }
  
});
