# frozen_string_literal: true

# Redmine plugin for Document Management System "Features"
#
# Vít Jonáš <vit.jonas@gmail.com>, Karel Pičman <karel.picman@kontron.com>
#
# This file is part of Redmine DMSF plugin.
#
# Redmine DMSF plugin is free software: you can redistribute it and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Redmine DMSF plugin 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 General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with Redmine DMSF plugin. If not, see
# <https://www.gnu.org/licenses/>.

# State controller
class DmsfStateController < ApplicationController
  menu_item :dmsf

  before_action :find_project
  before_action :authorize

  def user_pref_save
    member = @project.members.find_by(user_id: User.current.id)
    if member
      if Setting.notified_events.include?('dmsf_legacy_notifications')
        member.dmsf_mail_notification = params[:email_notify]
      end
      member.dmsf_title_format = params[:title_format]
      member.dmsf_fast_links = params[:fast_links].present?
      if format_valid?(member.dmsf_title_format) && member.save
        flash[:notice] = l(:notice_your_preferences_were_saved)
      else
        flash[:error] = l(:notice_your_preferences_were_not_saved)
      end
    else
      flash[:warning] = l(:user_is_not_project_member)
    end
    @project.update(dmsf_act_as_attachable: params[:act_as_attachable]) if RedmineDmsf.dmsf_act_as_attachable?
    @project.update default_dmsf_query_id: params[:default_dmsf_query]
    redirect_to settings_project_path(@project, tab: 'dmsf')
  end

  private

  def format_valid?(format)
    format.blank? || (/%(t|d|v|i|r)/.match?(format) && format.length < 256)
  end
end
