{% macro splice_junctions_button(institute_id, case_name, variant_id) %}
  <a class="btn btn-secondary btn-sm text-white mt-1" href="{{url_for('alignviewers.sashimi_igv', institute_id=institute_id, case_name=case_name, variant_id=variant_id)}}" target="_blank"
    data-toggle="tooltip" data-placement="top" title="Only available in build GRCh38">RNA splicing</a>
{% endmacro %}

{% macro variant_tag_button(variant, institute, case, manual_rank_options) %}
  <form action="{{ url_for('variant.variant_update', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" method="POST">
    <label class="mt-3">Variant tag</label>
    <div class="row">
      <div class="col-8">
        <select name="manual_rank" class="form-control">
          <option value="-1">Select a tag...</option>
          {% for rank, data in manual_rank_options.items() %}
            <option {% if rank == variant.manual_rank %} selected {% endif %} value="{{ rank }}">
              {{ data.name }}
            </option>
          {% endfor %}
        </select>
      </div>
      <div class="col-4">
        <button type="submit" name="manual_rank" class="btn btn-outline-secondary form-control">Save</button>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro variant_tier_button(variant, institute, case, cancer_tier_options) %}
  <form action="{{ url_for('variant.variant_update', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" method="POST">
    <label class="mt-3">Variant tier</label>
    <div class="row">
      <div class="col-8">
        <select name="cancer_tier" class="form-control" data-width="100%">
          <option value="-1">Select a tag...</option>
          {% for rank, data in cancer_tier_options.items() %}
            <option {% if rank == variant.cancer_tier %} selected {% endif %} value="{{ rank }}">
              {{ data.label }}
            </option>
          {% endfor %}
        </select>
      </div>
      <div class="col-4">
        <button type="submit" name="manual_rank" class="btn btn-outline-secondary form-control">Save</button>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro dismiss_variant_button(variant, institute, case, dismiss_variant_options) %}
  <form action="{{ url_for('variant.variant_update', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" method="POST">
    <label class="mt-3">Dismiss variant</label>
    <div class="row">
      <div class="col-8">
        <select multiple="multiple" name="dismiss_variant" id="dismiss_variant" class="selectpicker" data-width="100%">
          {% for rank, data in dismiss_variant_options.items() %}
            <option {% if rank~"" in variant.dismiss_variant %} selected {% endif %} value="{{ rank }}">
              {{ data.label }}
            </option>
          {% endfor %}
        </select>
      </div>
      <div class="col-4">
        <button type="submit" name="dismiss" class="btn btn-outline-secondary form-control">Save</button>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro mosaic_variant_button(variant, institute, case, mosaic_variant_options) %}
  <form action="{{ url_for('variant.variant_update', institute_id=institute._id, case_name=case.display_name, variant_id=variant._id) }}" method="POST">
    <label class="mt-3">Mosaic Tags</label>
    <div class="row">
      <div class="col-8">
        <select multiple="multiple" name="mosaic_tags" id="mosaic_tags" class="selectpicker" data-width="100%">
          {% for rank, data in mosaic_variant_options.items() %}
            <option {% if rank~"" in variant.mosaic_tags %} selected {% endif %} value="{{ rank }}">
              {{ data.label }}
            </option>
          {% endfor %}
        </select>
      </div>
      <div class="col-4">
        <button type="submit" name="mosaic" class="btn btn-outline-secondary form-control">Save</button>
      </div>
    </div>
  </form>
{% endmacro %}

{% macro igv_button(case, variant, case_groups) %}
  <form role="form" action="{{ url_for('alignviewers.igv') }}" target="_blank" method="POST">
    <!-- general hidden fields required to submit POST request to IGV viewer -->
    <input type="hidden" name="build" value="{{case.genome_build}}">
    <input type="hidden" name="contig" value="{{variant.chromosome}}">
    <input type="hidden" name="start" value="{{variant.position - 50}}">
    <input type="hidden" name="stop" value="{{variant.position + 50}}">
    <!-- end of general hidden form fields -->
    {% set aln = namespace(sample_names=[],mt_bams=[], mt_bais=[], bam_files=[], bai_files=[]) %}
    {% if variant.chromosome in ["MT","M"] %}
      {% if case_groups %}
        {% for group_id, case_group in case_groups.items() %}
            {% for grouped_case in case_group %}
              {% if grouped_case.mt_bams %}
                {% set aln.sample_names = aln.sample_names + grouped_case.sample_names %}
                {% set aln.mt_bams = aln.mt_bams + grouped_case.mt_bams %}
                {% set aln.mt_bais = aln.mt_bais + grouped_case.mt_bais %}
              {% endif %}
            {% endfor %}
        {% endfor %}
      {% else %}
        {% set aln.sample_names = case.sample_names %}
        {% set aln.mt_bams = case.mt_bams %}
        {% set aln.mt_bais = case.mt_bais %}
      {% endif %}
      {% if aln.mt_bams %}
        <input type="hidden" name="sample" value="{{aln.sample_names|join(',')}}">
        <input type="hidden" name="mt_bam" value="{{aln.mt_bams|join(',')}}">
        <input type="hidden" name="mt_bai" value="{{aln.mt_bais|join(',')}}">
        <button class="btn btn-sm btn-secondary" name="align" value="mt_bam" type=submit>IGV viewer</button>
      {% else %}
        <span class="text-muted">MT BAM file(s) missing</span>
      {% endif %}
    {% elif case.bam_files %}
      {% if case_groups %}
        {% for group_id, case_group in case_groups.items() %}
            {% for grouped_case in case_group %}
              {% if grouped_case.bam_files %}
                {% set aln.sample_names = aln.sample_names + grouped_case.sample_names %}
                {% set aln.bam_files = aln.bam_files + grouped_case.bam_files %}
                {% set aln.bai_files = aln.bai_files + grouped_case.bai_files %}
              {% endif %}
            {% endfor %}
        {% endfor %}
      {% else %}
        {% set aln.sample_names = case.sample_names %}
        {% set aln.bam_files = case.bam_files %}
        {% set aln.bai_files = case.bai_files %}
      {% endif %}
      <input type="hidden" name="sample" value="{{aln.sample_names|join(',')}}">
      <input type="hidden" name="bam" value="{{aln.bam_files|join(',')}}">
      <input type="hidden" name="bai" value="{{aln.bai_files|join(',')}}">
      {% if case.rhocall_beds %}
        <input type="hidden" name="rhocall_bed" value="{{case.rhocall_beds|join(',')}}">
        <input type="hidden" name="rhocall_wig" value="{{case.rhocall_wigs|join(',')}}">
      {% endif %}
      {% if case.tiddit_coverage_wigs %}
        <input type="hidden" name="tiddit_coverage_wig" value="{{case.tiddit_coverage_wigs|join(',')}}">
      {% endif %}
      {% if case.upd_regions_beds %}
        <input type="hidden" name="upd_regions_bed" value="{{case.upd_regions_beds|join(',')}}">
      {% endif %}
      {% if case.upd_sites_beds %}
        <input type="hidden" name="upd_sites_bed" value="{{case.upd_sites_beds|join(',')}}">
      {% endif %}
      <button class="btn btn-secondary btn-sm" name="align" value="bam" type="submit">IGV viewer</button>
    {% else %}
      <span class="text-muted">BAM file(s) missing</span>
    {% endif %}
  </form>
{% endmacro %}
