# Brimir is a helpdesk system to handle email support requests.
# Copyright (C) 2012-2016 Ivaldi https://ivaldi.nl/
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

class SettingsController < ApplicationController

  def edit
    @tenant = Tenant.current_tenant
    authorize! :edit, @tenant
  end

  def update
    @tenant = Tenant.current_tenant
    authorize! :update, @tenant

    if @tenant.update_attributes(tenant_params)
      redirect_to tickets_url, notice: I18n.t(:settings_saved)
    else
      render 'edit'
    end
  end

  protected

  def tenant_params
    params.require(:tenant).permit(
      :default_time_zone,
      :ignore_user_agent_locale,
      :default_locale,
      :share_drafts,
      :first_reply_ignores_notified_agents,
      :notify_client_when_ticket_is_assigned_or_closed,
      :notify_user_when_account_is_created,
      :notify_client_when_ticket_is_created,
      :ticket_creation_is_open_to_the_world,
      :ask_for_name,
      :stylesheet_url,
      :javascript_url,
      :always_notify_me,
      :work_can_wait
    )
  end

  def month_names
    @month_names = (1..12).map do |m|
      I18n.l DateTime.parse(Date::MONTHNAMES[m]),
          format: "%B"
    end
  end

  def day_names
    @day_names = (1..7).map do |m|
      I18n.l DateTime.parse(Date::MONTHNAMES[m])
    end
  end
end
