{% from "_helpers.html" import render_field, render_form_errors, copr_url, render_pypi_python_versions_field, render_additional_build_options, render_srpm_build_method_box with context %}
{% from "coprs/detail/_method_forms.html" import copr_method_form_fileds_custom %}

{% macro copr_package_form_begin(form, view, copr, package) %}
  {{ render_form_errors(form) }}
      {% if not package.name %}  {# Add package #}
        <form class="form-horizontal" action="{{ copr_url(view, copr, source_type_text=source_type_text) }}" method="post" enctype="multipart/form-data">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3 class="panel-title">{{ counter('instructions') }}. Provide the source</h3>
            </div>
            <div class="panel-body">
              {{ render_field(form.package_name) }}
      {% else %}  {# Edit package #}
        <form class="form-horizontal" action="{{ copr_url(view, copr, source_type_text=source_type_text, package_name=package.name) }}"
              method="post" enctype="multipart/form-data">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3 class="panel-title">{{ counter('instructions') }}. Provide the source</h3>
            </div>
            <div class="panel-body">
              <input type="hidden" name="package_name" value="{{ package.name }}" />
      {% endif %}
{% endmacro %}


{% macro copr_package_form_end(form, package, source_type_text) %}
  {{ form.csrf_token }}
    </div>
  </div>
  <p>
    You agree to build only <a href="https://docs.copr.fedorainfracloud.org/user_documentation.html#what-i-can-build-in-copr">allowed content</a> in Copr.
    Check if your <a href="https://fedoraproject.org/wiki/Licensing:Main?rd=Licensing#Good_Licenses">license</a> is allowed.
  </p>
  <input class="btn btn-primary pull-left" type="submit" value="Save">
  {% if package.name %}<input class="btn btn-default pull-right" type="submit" name="reset" value="Reset">{% endif %}
</form>
{% endmacro %}

{% macro render_webhook_rebuild(form) %}
  <div class="form-group">
    <label class="col-sm-2 control-label" for="textInput-markup">
      Auto-rebuild
    </label>
    <div class="col-sm-10">
      <input type="checkbox" name="webhook_rebuild" {% if form.webhook_rebuild.data == True %}checked="checked"{% endif %}/>
      Auto-rebuild the package? (i.e. every commit or new tag)
      | See <a href="{{ copr_url('coprs_ns.copr_integrations', copr) }}">Integrations</a>
    </div>
  </div>
{% endmacro %}

{% macro render_generic_pkg_form(form) %}
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">{{ counter('instructions') }}. Generic package setup</h3>
    </div>
    <div class="panel-body">
{{ render_field(form.chroot_denylist,
                placeholder="Optional - comma-separated list of wildcard-patterns, e.g.  fedora-*, *-i386",
                info="What chroots should be skipped for this package, by default we build for all.",
)}}
{{ render_field(form.max_builds)}}
{{ render_field(form.timeout)}}
{% endmacro %}

{% macro render_anitya_autorebuild(form) %}
  <div class="form-group">
    <label class="col-sm-2 control-label" for="textInput-markup">
      Anitya autorebuild:
    </label>
    <div class="col-sm-10 anitya-rebuild">
      <input type="checkbox" name="webhook_rebuild" id="anitya_rebuild" {% if form.webhook_rebuild.data == True %}checked="checked"{% endif %}/>
      <label for="webhook_rebuild" style="display:inline">Use Anitya emitted message to rebuild this package (you also need to register respective PyPI package on <a href="https://release-monitoring.org/">release-monitoring.org</a>). At the current state, Anitya update messages are emitted twice a day but you can also trigger a manual emission.</label>
    </div>
  </div>
{% endmacro %}

{% macro copr_package_delete_form(package, page, class="") %}
  <form class="{{ class }}" action="{{ copr_url('coprs_ns.copr_delete_package', package.copr, package_id=package.id)}}" method="post">
    <input type="hidden" value="page" value="{{ page }}">
    <button class="btn btn-default" type="submit" onclick="return confirm('Do you really want to remove the package?');">
      <span class="pficon pficon-delete"></span> Delete
    </button>
  </form>
{% endmacro %}

{% macro copr_package_form_pypi(form, view, copr, package) %}
    {{ copr_package_form_begin(form, view, copr, package) }}
    {{ render_field(form.pypi_package_name, placeholder="Package name in the Python Package Index.") }}

    {{ render_field(
         form.spec_generator,
         info="Tool for generating specfile from a PyPI package. The options "
               "are full-featured <strong>pyp2rpm</strong> with cross "
               "distribution support, and <strong>pyp2spec</strong> that is "
               "being actively developed and considered to be the future."
       )
    }}

    {# End the previous instructions box #}
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">
          {{ counter('instructions') }}. Options specific for pyp2rpm
        </h3>
      </div>
      <div class="panel-body">
        {{ render_field(
               form.spec_template,
               placeholder="Distribution specific spec template",
               info="Limited to <strong>pyp2rpm</strong> spec generator")
        }}
        {{ render_pypi_python_versions_field(form.python_versions) }}

    {{ render_generic_pkg_form(form) }}
    {{ render_anitya_autorebuild(form) }}
    {{ copr_package_form_end(form, package, 'rubygems') }}
{% endmacro %}


{% macro copr_package_form_rubygems(form, view, copr, package) %}
  {{ copr_package_form_begin(form, view, copr, package) }}
  {{ render_field(form.gem_name, placeholder="Gem name from RubyGems.org") }}
  {{ render_generic_pkg_form(form) }}
  {{ render_anitya_autorebuild(form) }}
  {{ copr_package_form_end(form, package, 'rubygems') }}
{% endmacro %}


{% macro copr_package_form_custom(form, view, copr, package) %}
  {{ copr_package_form_begin(form, view, copr, package) }}
  {{ copr_method_form_fileds_custom(form) }}
  {{ render_generic_pkg_form(form) }}
  {{ render_webhook_rebuild(form) }}
  {{ copr_package_form_end(form, package, 'custom') }}
{% endmacro %}


{% macro copr_package_form_distgit(form, view, copr, package) %}
  {{ copr_package_form_begin(form, view, copr, package) }}

  {{ render_field(form.distgit) }}
  {{ render_field(form.committish) }}
  {{ render_field(form.namespace) }}

  {{ render_generic_pkg_form(form) }}
  <div class="form-group">
    <label class="col-sm-2 control-label" for="textInput-markup">
      Auto-rebuild
    </label>
    <div class="col-sm-10">
      <input type="checkbox" name="webhook_rebuild" {% if form.webhook_rebuild.data == True %}checked="checked"{% endif %}/>
      Auto-rebuild the package on commits/PRs (currently supported only for
      fedora distgit packages) | See also
      <a href="{{ copr_url('coprs_ns.copr_integrations', copr) }}">Pagure Integration</a>
    </div>
  </div>
  {{ copr_package_form_end(form, package, 'disgit') }}
{% endmacro %}


{% macro copr_package_form_scm(form, view, copr, package) %}
  {{ copr_package_form_begin(form, view, copr, package) }}

  {{ render_field(form.scm_type) }}
  {{ render_field(form.clone_url, placeholder="URL to your Git or SVN repository") }}
  {{ render_field(form.committish, placeholder="Optional - Specific branch, tag, or commit that you want to build.") }}
  {{ render_field(form.subdirectory, placeholder="Optional - Subdirectory where source files and .spec are located.") }}
  {{ render_field(form.spec, placeholder="Optional - Path to your .spec file under the specified subdirectory.") }}

  {{ render_srpm_build_method_box(form) }}
  {{ render_generic_pkg_form(form) }}
  {{ render_webhook_rebuild(form) }}
  {{ copr_package_form_end(form, package, 'mock_scm') }}
{% endmacro %}

{% macro copr_packages_rebuild_all(form, view, copr) %}
  {# TODO: try to clean the the markup up, move into common helper methods etc.
     at least there should be a helper renderrer to render multicheckboxes,
     see also render_pypi_python_versions_field in _helpers.py #}
   {% if form.errors %}
   <div class="alert alert-danger" role="alert" style="margin-top:0px">
     <ul>
       {% for error, msg in form.errors.items() %}
       <li>{{ error }}: {{ msg[0] }}</li>
       {% endfor %}
     </ul>
   </div>
   {% endif %}
   <form class="form-horizontal" action="{{ copr_url(view, copr, source_type_text=source_type_text) }}"
        method="post" enctype="multipart/form-data">

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Select chroots and other options</h3>
      </div>
      <div class="panel-body">
      {{ render_additional_build_options(form, copr) }}
      </div>
    </div>

    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Select packages for rebuild</h3>
      </div>
      <div class="panel-body">
        <div class="form-group {% if form.errors %}has-error{% endif %}">
          <label class="col-sm-2 control-label" for="textInput-markup" style="text-align: left">
            Packages
          </label>
          <div class="col-sm-10" id="package-names">
          {% for choice in form.packages.choices %}
              <div class="checkbox">
                <label>
                <input id="{{ choice[0] }}" name="packages" value="{{ choice[1] }}" type="checkbox" {% if choice[1] in form.packages.data %}checked="checked"{% endif %}>
                <label for="{{ choice[0] }}">{{ choice[0] }}</label>
                </label>
              </div>
          {% endfor %}
          {% if form.packages.errors %}
            {% for error in form.packages.errors %}
              <li class="help-block">
                <span class="help-block">{{ error }}</span>
              </li>
            {% endfor %}
          {% endif %}
          </div>
        </div>
      </div>
    </div>

    <input class="btn btn-primary" type="submit" value="Build">
  </form>
{% endmacro %}
