<%# locals: (script_version:)  %>

<% if script_version.errors.empty? && script_version.script.errors.empty? %>
  <p>
    <%= t('scripts.rules_summary_html', full_rules_link: link_to(t('scripts.rules_summary_link_to_full'), help_code_rules_path)) %>
  </p>
<% end %>

<% if !script_version.script.new_record? && script_version.script.sync_type %>
  <p>
    <%= t('scripts.syncing_notice_html',
      syncing_notice_sync_type: link_to(
        t(
          script_version.script.sync_type_manual? ? 'scripts.syncing_notice_sync_type_manual' : 'scripts.syncing_notice_sync_type_automatic',
          sync_identifier: script_version.script.sync_identifier
        ),
        admin_script_path(script_version.script_id)
      )
    ) %>
  </p>
<% end %>

<%= form_for(script_version.script.new_record? ? script_version : [script_version.script, script_version], {html: {enctype: 'multipart/form-data'}}) do |s| %>

  <% # remember previously chosen overrides
  [:version_check_override, :add_missing_version, :namespace_check_override, :add_missing_namespace, :minified_confirmation, :allow_code_previously_posted, :meta_not_at_start_confirmation].each do |override|
    if script_version.send(override) %>
      <%= s.hidden_field(override) %>
    <% end
  end %>

  <%= hidden_field_tag(:language, script_version.script.language) if script_version.script.new_record? %>

  <% unless script_version.errors.empty? && script_version.script.errors.empty? %>
    <div class="validation-errors">
      <p>
        <%= t('common.error_preamble') %>
      </p>
      <% displayed_errors = []
      script_version.errors.each do |error|
        msg = error.message
        attr = error.attribute
        # need custom handling for warnings
        next if msg.starts_with?('warning-') || attr.to_s.include?('screenshot')
        displayed_errors << if attr == :base
          if msg == 'appears to be an unauthorized copy' && script_version.unauthorized_original
            it('scripts.error_unauthorized_copy',
              script: render_script(script_version.unauthorized_original),
              rules_link: help_code_rules_path,
              site_name: site_name
            )
          else
            msg
                              end
        elsif msg.is_a?(String)
          "#{attr.to_s.capitalize} #{msg}"
        else
          script_version.errors.generate_message(attr, msg)
                            end
      end
      script_version.script.errors.full_messages.each do |m|
        displayed_errors << m
      end
      displayed_errors.uniq.each do |err| %>
        <p><%= err %></p>
      <% rescue Encoding::CompatibilityError => ex
        Sentry.capture_exception(ex, extra: err)
      end
      script_version.warnings.each do |w| %>
        <p><%
          case w
            when :version_not_incremented
            %>
              <%= t('scripts.warning_version_not_incremented_html') %><br>
              <%= s.check_box(:version_check_override, {}, true, false) %> <label for="script_version_version_check_override"><%= t('scripts.override_warning_checkbox_label') %></label>
            <% when :version_missing %>
              <%= t('scripts.warning_version_missing_html') %><br>
              <%= s.check_box(:add_missing_version, {}, true, false) %> <label for="script_version_add_missing_version"><%= t('scripts.warning_version_missing_generate_automatically_html') %></label>
            <% when :namespace_missing %>
              <%= t('scripts.warning_namespace_missing_html') %><br>
              <%= s.check_box(:add_missing_namespace, {}, true, false) %> <label for="script_version_add_missing_namespace"><%= t('scripts.warning_namespace_missing_generate_automatically_html') %></label>
            <% when :namespace_changed %>
              <%= t('scripts.warning_namespace_changed_html') %><br>
              <%= s.check_box(:namespace_check_override, {}, true, false) %> <label for="script_version_namespace_check_override"><%= t('scripts.override_warning_checkbox_label') %></label>
            <% when :potentially_minified %>
              <%= t('scripts.warning_potentially_minified_html', allowed_requires_link: link_to(t('scripts.warning_potentially_minified_allowed_requires_link'), help_external_scripts_path)) %><br>
              <%= s.check_box(:minified_confirmation, {}, true, false) %> <label for="script_version_minified_confirmation"><%= t('scripts.override_warning_checkbox_label') %></label>
            <% when :automatic_sensitive %>
              <%= t('scripts.warning_automatic_sensitive_html', sensitive_domains: script_version.sensitive_domains.to_sentence) %><br>
              <%= s.check_box(:sensitive_site_confirmation, {}, true, false) %> <label for="script_version_sensitive_site_confirmation"><%= t('scripts.override_warning_checkbox_label') %></label>
            <% when :code_previously_posted %>
              <%= t('scripts.warning_code_previously_posted_html', script_links: script_version.previously_posted_scripts.map { |s| render_script(s) }.to_sentence.html_safe) %><br>
              <%= s.check_box(:allow_code_previously_posted, {}, true, false) %> <label for="script_version_allow_code_previously_posted"><%= t('scripts.override_warning_checkbox_label') %></label>
            <% when :license_missing %>
              <%= it('scripts.warning_license_missing_html', site_name: site_name, license_chooser_link: It.link('https://choosealicense.com/', target: 'license_chooser')) %><br>
              <%= s.check_box(:license_missing_override, {}, true, false) %> <label for="script_version_license_missing_override"><%= t('scripts.warning_license_missing_override_html') %></label>
            <% when :meta_not_at_start %>
              <%= t('scripts.warning_meta_not_at_start_html') %><br>
              <%= s.check_box(:meta_not_at_start_confirmation, {}, true, false) %> <label for="script_version_meta_not_at_start_confirmation"><%= t('scripts.override_warning_checkbox_label') %></label>
            <% else
              raise "Unknown warning #{w}"
          end %>
        </p>
      <% end %>
    </div>
  <% end %>

  <%= render partial: 'script_type', locals: { script_version: @script_version } if @script_version.script.new_record? %>

  <div id="script-library-inputs" class="form-section">
    <div class="form-control">
      <label for="library-name"><%= t('activerecord.attributes.script.name') %></label><br>
      <input id="library-name" name="name" value="<%= @script.name %>" maxlength="100">
    </div>
    <div class="form-control">
      <label for="library-description"><%= t('activerecord.attributes.script.description') %></label><br>
      <input id="library-description" name="description" value="<%= @script.description %>" maxlength="500">
    </div>
  </div>

  <% if @script.new_record? && @script.sync_identifier %>
    <%= hidden_field_tag :import_url, params[:import_url] %>
    <%= hidden_field_tag :sync_type, params[:sync_type] %>
  <% end %>


  <div class="form-section">
    <div class="form-control">
      <label for="script_version_code"><%= t('activerecord.attributes.script.code') %></label> <span class="label-note" style="display: none"><input type="checkbox" id="enable-source-editor-code" name="enable-source-editor-code" <% if params['enable-source-editor-code'] %> checked<% end %> class="enable-source-editor" data-related-editor="script_version_code" data-editor-language="<%= script_version.css? ? 'css' : 'javascript' %>"><label for="enable-source-editor-code" class="checkbox-label"><%= t('scripts.enable_source_code_editor_label') %></label></span><br>
      <%= s.text_area(:code) %>
      <div><%= t('scripts.upload_label') %> <input type="file" name="code_upload" id="code-upload" accept="text/javascript,application/javascript"></div>
    </div>
  </div>

  <div class="form-section">
    <% # default first
       script_version.localized_attributes_for('additional_info').sort do |a,b|
         next -1 if a.attribute_default && !b.attribute_default
         next 1 if !a.attribute_default && b.attribute_default
         next 0
       end.each_with_index do |la, index| %>
      <%= render partial: 'additional_info', locals: {la: la, index: index} %>
    <% end %>

    <div class="form-control">
      <button id="add-additional-info" name="add-additional-info" value="1" type="submit" data-form-path="<%= script_version_additional_info_form_path %>"><%= t('scripts.additional_info_add_localized') %></button>
    </div>

    <div class="script-screenshot-control">
      <%= render partial: 'screenshots/entry', locals: { f: s, current_attachments: @current_attachments } %>
    </div>
  </div>

  <% if !script_version.script.new_record? %>
    <div class="form-section">
      <div class="form-control">
        <label for="script_version_changelog"><%= t('activerecord.attributes.script.changelog') %></label> <span class="label-note"><%= t('scripts.changelog_note') %></span><br>
        <%= with_user_text_preview_for_form(form: s, markup_name: :changelog_markup) do
          s.text_area :changelog, rows: 5
        end %>
      </div>
    </div>
  <% end %>

  <%= render partial: 'script_type', locals: { script_version: @script_version } unless @script_version.script.new_record? %>

  <% if sleazy? %>
    <div class="form-section">
      <div class="form-control">
        <label><%= t('scripts.adult_content') %></label><br>
        <%= it('scripts.adult_content_notice_from_sleazy', site_name:, greasyfork_link: @script.new_record? ? new_script_version_url(host: greasyfork_host) : new_script_script_version_url(@script, host: greasyfork_host)) %>
      </div>
    </div>
  <% else %>
    <div class="form-section">
      <% if !script_version.script.sensitive || script_version.script.adult_content_self_report %>
        <div class="form-control radio-group">
          <label><%= t('scripts.adult_content') %></label><br>
          <%= t('scripts.adult_content_self_report_explanation', site_name: site_name) %><br>
          <%= check_box('script', :adult_content_self_report) %>
          <label for="script_adult_content_self_report" class="radio-label"><%= t('scripts.adult_content_self_report') %></label>
        </div>
      <% else %>
        <div class="form-control radio-group">
          <label><%= t('scripts.adult_content') %></label><br>
          <% if @script.for_sensitive_site? %>
            <%= t('scripts.not_adult_content_self_report_explanation_automatic', site_name: site_name, domain_list: @script.matching_sensitive_sites.pluck(:domain).to_sentence) %><br>
          <% else %>
            <%= t('scripts.not_adult_content_self_report_explanation', site_name: site_name) %><br>
            <%= check_box('script', :not_adult_content_self_report, checked: @script.not_adult_content_self_report_date.present?) %>
            <label for="script_not_adult_content_self_report" class="radio-label"><%= t('scripts.not_adult_content_self_report_html') %></label>
          <% end %>
        </div>
      <% end %>
    </div>
  <% end %>

  <% if @script.css? %>
    <div class="form-section">
      <div class="form-control radio-group">
        <label><%= t('scripts.not_js_convertible_override_header') %></label><br>
        <%= it('scripts.not_js_convertible_override_explanation', site_name: site_name, help_link: help_user_js_conversions_path) %><br>
        <%= s.check_box :not_js_convertible_override %>
        <%= s.label :not_js_convertible_override, t('scripts.not_js_convertible_override_label_html'), class: 'radio-label' %>
      </div>
    </div>
  <% end %>

  <% if @script.new_record? %>
    <div class="form-section">
      <div class="form-control radio-group">
        <label><%= t('activerecord.attributes.script.locale') %></label> <span class="label-note"><%= t('scripts.locale_explanation_html') %></span><br>
        <%= render partial: 'scripts/default_locale_selector', locals: {script: @script} %>
      </div>
    </div>
  <% end %>

  <%= flash[:recaptcha_error] %>

  <p>
    <% if @script.new_record? && UserRestrictionService.new(current_user).must_recaptcha? %>
      <%= invisible_recaptcha_tags callback: 'submitInvisibleRecaptchaFormScriptVersion', text: script_version.script.new_record? ? t('scripts.post_new_script') : t('scripts.post_updated_script') %>
    <% else %>
      <%= submit_tag script_version.script.new_record? ? t('scripts.post_new_script') : t('scripts.post_updated_script') %>
    <% end %>
  </p>
<% end %>
