/// QasTools: Desktop toolset for the Linux sound system ALSA.
/// \copyright See COPYING file.

#include "desktop_items_setup.hpp"
#include <QSettings>
#include <iostream>

Desktop_Items_Setup::Desktop_Items_Setup ()
: Utility::Setup ( nullptr )
, tray ( this )
, main_window ( this )
, settings_dialog ( this )
, _start_user_device ( QLatin1StringView ( "hw:0" ) )
{
}

Desktop_Items_Setup::~Desktop_Items_Setup () = default;

bool
Desktop_Items_Setup::set_start_device_mode ( Mixer_Device mode_n )
{
  return change ( _start_device_mode, mode_n );
}

bool
Desktop_Items_Setup::set_start_user_device ( const QString & device_n )
{
  return change ( _start_user_device, device_n );
}

bool
Desktop_Items_Setup::set_tray_on_close ( bool flag_n )
{
  return change ( _tray_on_close, flag_n );
}

bool
Desktop_Items_Setup::set_tray_show_icon ( bool flag_n )
{
  return change ( _tray_show_icon, flag_n );
}

bool
Desktop_Items_Setup::set_tray_is_minimized ( bool flag_n )
{
  return change ( _tray_is_minimized, flag_n );
}

void
Desktop_Items_Setup::read_from_storage ()
{
  Main_Window_Setup & mwin = main_window;

  QSettings settings;

  set_start_device_mode ( static_cast< Desktop_Items_Setup::Mixer_Device > (
      settings.value ( "start_device_mode", start_device_mode () )
          .toUInt () ) );

  set_start_user_device (
      settings.value ( "start_user_device", start_user_device () )
          .toString () );

  mwin.mixer_dev.set_control_address (
      settings
          .value ( "current_device",
                   mwin.mixer_dev.control_address ().addr_str () )
          .toString () );

  mwin.inputs.set_wheel_degrees (
      settings.value ( "wheel_degrees", mwin.inputs.wheel_degrees () )
          .toUInt () );

  set_tray_on_close (
      settings.value ( "tray_on_close", tray_on_close () ).toBool () );

  set_tray_show_icon (
      settings.value ( "tray_show_icon", tray_show_icon () ).toBool () );

  set_tray_is_minimized (
      settings.value ( "tray_is_minimized", tray_is_minimized () ).toBool () );

  // Main window state
  {
    settings.beginGroup ( "main_window" );

    mwin.set_window_state (
        settings.value ( "window_state", mwin.window_state () )
            .toByteArray () );

    mwin.set_window_geometry (
        settings.value ( "window_geometry", mwin.window_geometry () )
            .toByteArray () );

    mwin.set_splitter_state (
        settings.value ( "splitter_state", mwin.splitter_state () )
            .toByteArray () );

    mwin.set_show_dev_select (
        settings.value ( "show_device_selection", mwin.show_dev_select () )
            .toBool () );

    settings.endGroup ();
  }

  // Device selection
  {
    Views::Device_Selection_View_Setup & vsetup ( main_window.dev_select );

    settings.beginGroup ( "device_selection" );

    vsetup.set_user_device (
        settings.value ( "user_device", vsetup.user_device () ).toString () );

    settings.endGroup ();
  }

  // Mixer setup
  {
    Views::Mixer_Setup & vsetup ( main_window.mixer );

    settings.beginGroup ( "simple_mixer" );

    vsetup.set_show_slider_value_labels (
        settings
            .value ( "show_slider_value_labels",
                     vsetup.show_slider_value_labels () )
            .toBool () );

    vsetup.set_show_tool_tips (
        settings.value ( "show_tool_tips", vsetup.show_tool_tips () )
            .toBool () );

    settings.endGroup ();
  }

  // Tray mixer
  {
    settings.beginGroup ( "tray_mixer" );

    tray.set_device_mode ( static_cast< Tray::Setup::Mixer_Device > (
        settings.value ( "device_mode", tray.device_mode () ).toInt () ) );

    tray.set_user_device (
        settings.value ( "user_device", tray.user_device () ).toString () );

    tray.set_balloon_enabled (
        settings.value ( "show_balloon", tray.balloon_enabled () ).toBool () );

    tray.set_balloon_lifetime (
        settings.value ( "balloon_lifetime", tray.balloon_lifetime () )
            .toUInt () );

    tray.set_wheel_degrees (
        settings.value ( "wheel_degrees", tray.wheel_degrees () ).toUInt () );

    tray.set_show_volume_icon (
        settings.value ( "show_volume_icon", tray.show_volume_icon () )
            .toBool () );

    settings.endGroup ();
  }

  // Settings dialog
  {
    settings.beginGroup ( "settings_dialog" );

    settings_dialog.set_page (
        settings
            .value ( "page",
                     static_cast< unsigned int > ( settings_dialog.page () ) )
            .toUInt () );

    settings_dialog.set_window_geometry (
        settings.value ( "window_geometry", settings_dialog.window_geometry () )
            .toByteArray () );

    settings_dialog.set_splitter_state (
        settings.value ( "splitter_state", settings_dialog.splitter_state () )
            .toByteArray () );

    settings.endGroup ();
  }

  // Device settings dialog
  {
    settings.beginGroup ( "device_settings_dialog" );

    mwin.device_settings_dialog.set_page (
        settings
            .value ( "page",
                     static_cast< unsigned int > (
                         mwin.device_settings_dialog.page () ) )
            .toUInt () );

    mwin.device_settings_dialog.set_window_geometry (
        settings
            .value ( "window_geometry",
                     mwin.device_settings_dialog.window_geometry () )
            .toByteArray () );

    mwin.device_settings_dialog.set_splitter_state (
        settings
            .value ( "splitter_state",
                     mwin.device_settings_dialog.splitter_state () )
            .toByteArray () );

    mwin.device_settings_dialog.set_controls_header_view_state (
        settings
            .value ( "controls_header_view_state",
                     mwin.device_settings_dialog.controls_header_view_state () )
            .toByteArray () );

    settings.endGroup ();
  }

  // Sanitize values
  if ( start_device_mode () > Desktop_Items_Setup::MIXER_DEV_LAST ) {
    set_start_device_mode ( Desktop_Items_Setup::MIXER_DEV_DEFAULT );
  }
  if ( main_window.inputs.wheel_degrees () == 0 ) {
    main_window.inputs.set_wheel_degrees ( 720 );
  }
  if ( tray.device_mode () > Tray::Setup::MIXER_DEV_LAST ) {
    tray.set_device_mode ( Tray::Setup::MIXER_DEV_DEFAULT );
  }
  if ( tray.wheel_degrees () == 0 ) {
    tray.set_wheel_degrees ( 720 );
  }

  // Update storage change id
  _storage_change_id = change_id ();
}

void
Desktop_Items_Setup::write_to_storage ()
{
  // Check if there were changes at all
  if ( !differs_from_storage () ) {
    // std::cout << "Desktop_Items_Setup::write_to_storage: No changes\n ";
    return;
  }
  // std::cout << "Desktop_Items_Setup::write_to_storage: Writing changes\n";

  QSettings settings;

  // General

  settings.setValue ( "start_device_mode", start_device_mode () );

  settings.setValue ( "start_user_device", start_user_device () );

  settings.setValue ( "current_device",
                      main_window.mixer_dev.control_address ().addr_str () );

  settings.setValue ( "wheel_degrees", main_window.inputs.wheel_degrees () );

  settings.setValue ( "tray_on_close", tray_on_close () );

  settings.setValue ( "tray_show_icon", tray_show_icon () );

  settings.setValue ( "tray_is_minimized", tray_is_minimized () );

  // Main window state
  {
    settings.beginGroup ( "main_window" );

    settings.setValue ( "window_state", main_window.window_state () );

    settings.setValue ( "window_geometry", main_window.window_geometry () );

    settings.setValue ( "splitter_state", main_window.splitter_state () );

    settings.setValue ( "show_device_selection",
                        main_window.show_dev_select () );

    settings.endGroup ();
  }

  // Device selection
  {
    const Views::Device_Selection_View_Setup & vsetup = main_window.dev_select;

    settings.beginGroup ( "device_selection" );

    settings.setValue ( "user_device", vsetup.user_device () );

    settings.endGroup ();
  }

  // Simple mixer
  {
    const Views::Mixer_Setup & vsetup ( main_window.mixer );

    settings.beginGroup ( "simple_mixer" );

    settings.setValue ( "show_slider_value_labels",
                        vsetup.show_slider_value_labels () );

    settings.setValue ( "show_tool_tips", vsetup.show_tool_tips () );

    settings.endGroup ();
  }

  // Mini mixer
  {
    settings.beginGroup ( "tray_mixer" );

    settings.setValue ( "device_mode", tray.device_mode () );

    settings.setValue ( "user_device", tray.user_device () );

    settings.setValue ( "show_balloon", tray.balloon_enabled () );

    settings.setValue ( "balloon_lifetime", tray.balloon_lifetime () );

    settings.setValue ( "wheel_degrees", tray.wheel_degrees () );

    settings.setValue ( "show_volume_icon", tray.show_volume_icon () );

    settings.endGroup ();
  }

  // Settings dialog
  {
    settings.beginGroup ( "settings_dialog" );

    settings.setValue (
        "page", static_cast< unsigned int > ( settings_dialog.page () ) );

    settings.setValue ( "window_geometry", settings_dialog.window_geometry () );

    settings.setValue ( "splitter_state", settings_dialog.splitter_state () );

    settings.endGroup ();
  }

  // Device settings dialog
  {
    settings.beginGroup ( "device_settings_dialog" );

    settings.setValue ( "page",
                        static_cast< unsigned int > (
                            main_window.device_settings_dialog.page () ) );

    settings.setValue ( "window_geometry",
                        main_window.device_settings_dialog.window_geometry () );

    settings.setValue ( "splitter_state",
                        main_window.device_settings_dialog.splitter_state () );

    settings.setValue (
        "controls_header_view_state",
        main_window.device_settings_dialog.controls_header_view_state () );

    settings.endGroup ();
  }

  // Update storage change id
  _storage_change_id = change_id ();
}
