<div>
  {{log "roll-sr6.html this=" this}}
  {{log "roll-sr6.html data=" data}}
  {{log "roll-sr6.html prepared=" prepared}}
  {{log "roll-sr6.html configured=" configured}}

  {{#if configured.extended}}<h3>{{localize 'shadowrun6.label.extended_test'}}</h3>{{/if}}
  <h3>{{actionText}}</h3>


  {{#if (or (or edge_use edge_use_opponent) isOpposed)}}
  <div class="chat-edge" style="display:block">    
    {{#if isOpposed}}
    <div>{{edge_message}}</div>
    <div class="chat-edge-collapsibleDISABLED" style="display:none">
      <header class="part-header flexrow">
        <div>{{ localize 'shadowrun6.label.attack_rating'}}: <b>{{attackRating}}</b></div>
        <br/>
        <div>{{ localize 'shadowrun6.label.defense_rating'}}: <b>{{defRating}}</b></div>
      </header>
    </div>
    {{/if}}
    {{#if edge_use}}
    <div style="color: var(--highlight-color)">{{edge_use}}</div>
    {{/if}}
    {{#if edge_use_opponent}}
    <div style="color: var(--highlight-color)">{{edge_use_opponent}}</div>
    {{/if}}
  </div>
  <hr/>
  {{/if}}

  {{#if publicRoll}}
  <div>
    <div class="dice-result">
      <header class="part-header flexrow{{#unless (eq rollType "initiative")}} chat-edge-post{{/unless}}">
        <div class="resulttext">{{#if (not configured.rollType)}}/r {{/if}}{{formula}}</div>
        <br/>
        {{#if system.extended}}
            <div class="resulttext" style="flex:2;text-align: right;">{{ localize 'shadowrun6.roll.accumulated' }}: <b>{{system.extendedAccumulate}}</b> / {{system.threshold}}</div>
        {{else}}
        		{{#if (eq rollType "initiative")}}
        		<div class="resulttext" style="flex:2">{{localize 'shadowrun6.section.karma.total'}}: {{total}}</div>
        		{{else}}
              {{#if (and configured.extended (not configured.threshold))}}
                  <div class="resulttext" style="flex:2">{{total}} {{localize 'shadowrun6.roll.hits'}}
                  </div>
              {{else}}
                {{#if (not configured.rollType)}}
                  {{!-- Manual FoundryVTT /r --}}
                  <div class="resulttext" style="flex:2">                  
                    {{localize 'shadowrun6.roll.manualResult'}}: <b>{{total}}</b>
                  </div>
                {{else}}
                  {{#if success}}
                    <div class="resulttext" style="flex:2">                  
                      {{localize 'shadowrun6.roll.success'}}: <b>{{total}}</b> {{localize 'shadowrun6.roll.successes'}}{{#if netHits}}, <b>{{netHits}}</b> {{localize 'shadowrun6.roll.netHits'}}{{/if}}
                    </div>
                  {{else}}
                    <div class="resulttext" style="flex:2">
                      {{ localize 'shadowrun6.roll.failure'}}: </b>{{total}}</b> {{localize 'shadowrun6.roll.hits'}}
                    </div>
                  {{/if}}
                {{/if}}
              {{/if}}
            {{/if}}
        {{/if}}
        <br/>
        {{#if criticalglitch}}
        <span style="color: rgb(215, 14, 233); flex:2">{{localize 'shadowrun6.roll.criticalglitch'}}</span><br/>
        {{else glitch}}
        <span style="color: rgb(194, 76, 29); flex:2">{{localize 'shadowrun6.roll.glitch'}}</span><br/>
        {{/if}}
      </header>
        {{{tooltip}}}
    </div>
   </div>

   
  {{#if configured.extended}}
   <div class="chat-extended-test">
      <hr>
      {{#if configured.threshold}}
        <div class="{{#if success}}success{{else}}failure{{/if}}" > {{extendedResultMsg}} </div>
      {{else}}
        <div class="continue-extended-test">
          <div class="resulttext success">{{extendedResultMsg}}</div>
          <button class="rollable" data-roll-type="continue_extended" data-pool="{{configured.pool}}" data-interval="{{configured.interval}}" data-interval-scale="{{configured.intervalScale}}" 
                  data-time-passed="{{configured.timePassed}}" data-hits="{{configured.extendedTotal}}" data-action-text="{{configured.actionText}}" 
                  data-targetid="{{speaker.token}}" data-actorId="{{speaker.actor}}" data-sceneId="{{speaker.scene}}">{{localize 'shadowrun6.dice.extended.continue'}}?
          </button>
        </div>
      {{/if}}
    </div>
  {{/if}}
  
   {{#if (eq configured.skillSpec "sprinting")}}
   <!-- Sprinting Details -->
   <div class="chat-sprinting">
      <hr>
      <h4>{{ configured.sprintingResult }}</h4>
   </div>
   {{/if}}

   {{#if (eq rollType "legwork")}}
   <!-- Legwork Details -->
   <div class="chat-legwork">
      <hr>
      <h4>{{ configured.checkText }}</h4>
      {{#if configured.legwork.loyalty}}
        <ul>
          {{#iff total ">=" 10}}<li>{{localize 'shadowrun6.legwork.results.10' }}</li>{{/iff}}
          {{#iff total ">=" 9}}<li>{{localize 'shadowrun6.legwork.results.9' }}</li>{{/iff}}
          {{#iff total ">=" 8}}<li>{{localize 'shadowrun6.legwork.results.8' }}</li>{{/iff}}
          {{#iff total ">=" 7}}<li>{{localize 'shadowrun6.legwork.results.7' }}</li>{{/iff}}
          {{#iff total ">=" 6}}<li>{{localize 'shadowrun6.legwork.results.6' }}</li>{{/iff}}
          {{#iff total ">=" 5}}<li>{{localize 'shadowrun6.legwork.results.5' }}</li>{{/iff}}
          {{#iff total ">=" 4}}<li>{{localize 'shadowrun6.legwork.results.4' }}</li>{{/iff}}
          {{#iff total ">=" 3}}<li>{{localize 'shadowrun6.legwork.results.3' }}</li>{{/iff}}
          {{#iff total ">=" 2}}<li>{{localize 'shadowrun6.legwork.results.2' }}</li>{{/iff}}
          {{#iff total ">=" 1}}<li>{{localize 'shadowrun6.legwork.results.1' }}</li>{{/iff}}
          {{#iff total "==" 0}}<li>{{localize 'shadowrun6.legwork.results.0' }}</li>{{/iff}}
        </ul>
        <hr>
        <button class="rollable loyalty-roll" data-roll-type="legwork" data-loyalty="{{configured.legwork.loyalty}}" data-contact="{{configured.legwork.contact}}" data-legwork-result="{{total}}"
                data-targetid="{{configured.speaker.token}}" data-actorId="{{configured.speaker.actor}}" data-sceneId="{{configured.speaker.scene}}">
          {{localize 'shadowrun6.contact.loyalty'}} ({{configured.legwork.loyalty}})
        </button>
      {{else}}
        <ul>
          {{#iff legwork.legworkResult ">=" 10}}{{#iff total ">=" 10}}<li>{{localize 'shadowrun6.legwork.results.10' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 9}}{{#iff total ">=" 9}}<li>{{localize 'shadowrun6.legwork.results.9' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 8}}{{#iff total ">=" 8}}<li>{{localize 'shadowrun6.legwork.results.8' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 7}}{{#iff total ">=" 7}}<li>{{localize 'shadowrun6.legwork.results.7' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 6}}{{#iff total ">=" 6}}<li>{{localize 'shadowrun6.legwork.results.6' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 5}}{{#iff total ">=" 5}}<li>{{localize 'shadowrun6.legwork.results.5' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 4}}{{#iff total ">=" 4}}<li>{{localize 'shadowrun6.legwork.results.4' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 3}}{{#iff total ">=" 3}}<li>{{localize 'shadowrun6.legwork.results.3' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 2}}{{#iff total ">=" 2}}<li>{{localize 'shadowrun6.legwork.results.2' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult ">=" 1}}{{#iff total ">=" 1}}<li>{{localize 'shadowrun6.legwork.results.1' }}</li>{{/iff}}{{/iff}}
          {{#iff legwork.legworkResult "==" 0}}{{#iff total "==" 0}}<li>{{localize 'shadowrun6.legwork.results.0' }}</li>{{/iff}}{{/iff}}
        </ul>
        {{#iff legwork.legworkResult ">" total}}
          <hr>
          <div class="payformore">{{localize 'shadowrun6.legwork.payformore' }}</div>
        {{/iff}}
      {{/if}}
   </div>
   {{/if}}

   {{#if (eq rollType "weapon")}}
   <!-- Usage Details -->
   <div class="chat-weapon">
   	    <table class="spell-table">
   	      <tr>
   	    	  <th align="center">{{localize 'shadowrun6.weapon.damage'}}</th>
   	    	  {{#if configured.fireMode}}
              <th align="center">{{localize 'shadowrun6.weapon.firemodes'}}</th>
              {{/if}}
              <th align="center">{{localize 'shadowrun6.weapon.attackRating'}}</th>
   	    	  {{#if configured.fireMode}}
              <th align="center">{{localize 'shadowrun6.item.ammunition'}}</th>
              {{/if}}
   	      </tr>
   	  	   <tr>
    	  		  <td><span name="calcDamage">{{configured.calcDamage}}</span>{{#if (eq configured.monitor "stun")}}S{{else}}P{{/if}}</td>
   	    	  {{#if configured.fireMode}}
   	  		  <td>{{localize (concat "shadowrun6.item.firemode." configured.fireMode)}}</td>
              {{/if}}
    	  		  <td name="calcAR">{{{attackrating configured.calcAttackRating}}}</td>
   	    	  {{#if configured.fireMode}}
   	  		  <td name="calcRounds">{{configured.calcRounds}}</td>
              {{/if}}
   	  	   </tr>
   	    </table>
   </div>
   {{/if}}
   
   {{#if system.matrixAction}}
   <!-- Matrix -->
   <hr/>
   <div>
     <b>{{localize 'shadowrun6.roll.damage'}}</b> To Do
   </div>
   {{/if}}

   {{#if (eq rollType "spell")}}
   <!-- Spell -->
   <hr/>
   <div class="chat-spell">
   	<table class="spell-table" style="text-align: center">
   	    <tr>
   	    	  <th>{{localize 'shadowrun6.spell.range'}}</th>
            <th>{{localize 'shadowrun6.spell.type'}}</th>
            <th>{{localize 'shadowrun6.spell.duration_short'}}</th>
            <th>{{localize 'shadowrun6.spell.drain'}}</th>
            {{#if (or threshold configured.spell.withEssence)}}
              {{#unless (or configured.calcDamage (eq defendWith "spells_direct"))}}
            <th>{{localize 'shadowrun6.roll.threshold'}}</th>
              {{/unless}}
            {{/if}}
            {{#if (or configured.calcDamage (eq defendWith "spells_direct"))}}
            <th>{{localize 'shadowrun6.spell.damage_short'}}</th>
            {{/if}}
   	    </tr>
   	  	<tr>
            <td>{{localize (concat (concat 'shadowrun6.spell.range_' configured.spell.range) "_short")}}</td>
            <td>{{localize (concat (concat 'shadowrun6.spell.type_' configured.spell.type) "_short")}}</td>
            <td>{{localize (concat (concat 'shadowrun6.spell.duration_' configured.spell.duration) "_short")}}</td>
            <td>{{configured.calcDrain}}</td>
            {{#if (or threshold configured.spell.withEssence)}}
              {{#unless (or configured.calcDamage (eq defendWith "spells_direct"))}}
            <td>{{threshold}}</td>
              {{/unless}}
            {{/if}}
            {{#if (or configured.calcDamage (eq defendWith "spells_direct"))}}
            <td>{{configured.calcDamage}}{{#if (or (eq configured.spell.damage "stun") (eq configured.spell.damage "stun_special"))}}S{{else}}P{{/if}}</td>
            {{/if}}
   	  		<td></td>
   	  	</tr>
   	</table>
		<div class="chat-spell-collapsible" style="display:none">
   		{{{configured.spellDesc}}}
		</div>
   </div>
   <div>
    <hr>
  	  <button class="rollable" data-roll-type="soak" data-soak="drain" data-damage="{{configured.calcDrain}}" data-monitor="stun" data-defend-with="{{../defendWith}}"  data-targetid="{{speaker.token}}" data-actorId="{{speaker.actor}}" data-sceneId="{{speaker.scene}}">
        {{localize 'shadowrun6.roll.button.drain'}} ({{configured.calcDrain}})
      </button>
    </div>
   {{/if}}

   {{#if (eq rollType "complexform")}}
   <!-- Complex Form -->
   <hr/>
   <div class="chat-spell">
   	<table class="spell-table" style="text-align: center">
   	    <tr>
            {{#if threshold}}
            <th>{{localize 'shadowrun6.roll.threshold'}}</th>
            {{/if}}
            <th>{{localize 'shadowrun6.complexform.fading'}}</th>
            <th>{{localize 'shadowrun6.complexform.duration'}}</th>
   	    </tr>
   	  	<tr>
            {{#if threshold}}
            <td>{{threshold}}</td>
            {{/if}}
    	  	  <td>{{configured.calcFade}}</td>
   	  		  <td>{{localize (concat (concat 'shadowrun6.spell.duration_' configured.form.duration) "_short")}}</td>
   	  	</tr>
   	</table>
		<div class="chat-spell-collapsible" style="display:none">
   		{{{configured.formDesc}}}
		</div>
   </div>
  	<button class="rollable" data-roll-type="soak" data-soak="fading" data-damage="{{configured.calcFade}}" data-monitor="stun" 
      data-defend-with="{{../defendWith}}" data-target-actor="{{speaker.actor}}" data-target-token="{{speaker.token}}">
      {{speaker.alias}}: {{localize 'shadowrun6.defense.fading'}} ({{configured.calcFade}} )
    </button>
   {{/if}}

   <!-- Defense possible -->
{{#if success}}
   {{#if (and defendWith (not cantDodgeBullets))}}
   {{log "************************Defense" this}}
   <hr/>
    {{log "**Defense targets " targetIds}}
    {{log "**Defense targets " targetIds.length}}
   {{#iff targets.length ">" 0}}
    <!-- TODO rework multiple targets so they have multiple rolls -->
     {{#each targets as |token id|}}
     <div>
      {{log "Token" id " = " token }}
     	<button class="rollable" data-roll-type="defense" 
        data-damage="{{../configured.calcDamage}}"  
        data-monitor="{{../monitor}}" 
        data-defend-with="{{../defendWith}}" 
        data-targetid="{{token.id}}" 
        data-actorId="{{token.actorId}}" 
        data-sceneId="{{token.sceneId}}" 
        data-threshold="{{../threshold}}"
        {{#if ../configured.itemUuid}} data-item-uuid="{{../configured.itemUuid}}" {{/if}}
      >
        {{localize 'shadowrun6.roll.button.defense'}} ({{../threshold}}): {{token.name}}
      </button>
     </div>
     {{/each}}
   {{else}}
     <!-- When no targets have been selected -->
     <div>
  	    <button class="rollable" data-roll-type="defense" 
          data-damage="{{configured.calcDamage}}"  
          data-monitor="{{monitor}}" 
          data-defend-with="{{defendWith}}" 
          data-targetid="{{targetId}}" 
          data-threshold="{{threshold}}"
          {{#if configured.itemUuid}} data-item-uuid="{{configured.itemUuid}}" {{/if}}
        >
          {{localize 'shadowrun6.roll.button.defense'}} ({{threshold}})
        </button>
     </div>
   {{/iff}}
   {{/if}}
{{/if}}
   
   {{#if (or (eq rollType "defense") cantDodgeBullets)}}
      {{#if actorTraits.hardenedArmor}}
        {{#if (not actorTraits.immunityNormalWeapons)}}
        <div class="resulttext" style="flex:2; margin-bottom: 5px; font-style:italic;">{{ localize 'shadowrun6.roll.hardenedArmor'}}</div>
        {{else}}
          {{#if (ne configured.defendedWith 'spells_indirect')}}
          <div class="resulttext" style="flex:2; margin-bottom: 5px; font-style:italic;">{{ localize 'shadowrun6.roll.hardenedArmor'}}</div>
          {{/if}}
        {{/if}}
      {{/if}}
      {{#if cantDodgeBullets}}
        {{#if success}}
          {{#iff targets.length ">" 0}}
          {{#each targets as |target|}}
            <div class="resulttext success" style="flex:2; margin-bottom: 5px;">{{ localize 'shadowrun6.roll.succesfulHit'}}</div>
            <button class="rollable" data-roll-type="soak" data-soak="{{../soakType}}" data-damage="{{../damage}}" data-monitor="{{../monitor}}" data-targetid="{{target.id}}" data-actorId="{{target.actorId}}" data-sceneId="{{target.sceneId}}">{{target.name}}: {{localize 'shadowrun6.roll.button.soak'}} ({{../damage}}{{#if (eq ../soakType "damage_stun")}}S{{else}}P{{/if}})</button>
          {{/each}}
          {{else}}
            <div class="resulttext success" style="flex:2; margin-bottom: 5px;">{{ localize 'shadowrun6.roll.succesfulHit'}}</div>
            <button class="rollable" data-roll-type="soak" data-soak="{{soakType}}" data-damage="{{damage}}" data-monitor="{{monitor}}" >{{localize 'shadowrun6.roll.button.soak'}} ({{damage}}{{#if (eq soakType "damage_stun")}}S{{else}}P{{/if}})</button>
          {{/iff}}
        {{else}}
          <div class="resulttext failure" style="flex:2">{{ localize 'shadowrun6.roll.missedAttack'}}</div>
        {{/if}}
      {{else}}
        {{#if success}}
            <div class="resulttext success" style="flex:2;">
              {{#if (eq configured.defendedWith "item_defined")}}
              {{ localize 'shadowrun6.roll.successfulDefenseGeneral'}}
              {{else}}
              {{ localize 'shadowrun6.roll.successfulDefense'}}
              {{/if}}
            </div>
        {{else}}
            <div class="resulttext failure" style="flex:2;">
              {{#if (eq configured.defendedWith "item_defined")}}
              {{ localize 'shadowrun6.roll.failedDefenseGeneral'}}
              {{else}}
              {{ localize 'shadowrun6.roll.failedDefense'}} {{nettoDamage}}
              {{/if}}
            </div>
            {{#if monitor}}
            <button class="rollable" data-roll-type="soak" data-soak="{{soakType}}" data-damage="{{damage}}" data-monitor="{{monitor}}" data-targetid="{{speaker.token}}" data-actorId="{{speaker.actor}}" data-sceneId="{{speaker.scene}}">{{localize 'shadowrun6.roll.button.soak'}} ({{damage}}{{#if (eq soakType "damage_stun")}}S{{else}}P{{/if}})</button>
            {{/if}}
        {{/if}}
      {{/if}}
   {{/if}}

   <!-- After Soak / Apply Damage -->
   {{#if (eq rollType "soak")}}
   {{log "************************Soak" this}}
    {{#if monitor}}
    <!-- Soak Damage -->
    <hr>
    <div class="soak-result">
        {{#if success}}
          <div class="resulttext" style="flex:2">{{ localize 'shadowrun6.roll.successfulSoak'}}</div>
        {{else}}
            

              <header class="part-header flexrow chat-edge-post">
                <div style="display: flex; align-items: center;"><span>{{ localize 'shadowrun6.roll.failedSoak'}}</span></div>
                <br/>
                {{#if (eq monitor 'physical')}}
                  <div class="resulttext" style="flex:2; display: flex; flex-direction: column;">
                    <span class="failure" style="line-height: 2em; font-size: 1.2em;">{{damage}} {{localize (concat 'shadowrun6.monitor.' monitor)}}</span>
                    {{#if damageConvertedStun}}
                    <span style="color: dodgerblue; font-size: 1.2em; font-weight: bold;">{{damageConvertedStun}} {{localize 'shadowrun6.monitor.stun'}}</span>
                    {{/if}}
                  </div>
                {{else}}
                  <div class="resulttext" style="flex:2;">
                    <span style="color: dodgerblue; line-height: 2em; font-size: 1.2em; font-weight: bold;">{{damage}} {{localize (concat 'shadowrun6.monitor.' monitor)}}</span>
                  </div>
                {{/if}}
              </header>

              <hr>

              {{#if (or (eq actor.type "Spirit") (eq actor.type "Vehicle"))}}
                  <!-- Spirit and vehicle damage not yet automated; Spirits have astral form and no stun, while vehicle stun is matrix damage -->
                  <span style="font-style: italic;">{{localize 'shadowrun6.ui.notifications.apply_manually'}}</span>
              {{else}}
                  <button class="rollable{{#if damageApplied}} damageApplied{{/if}}" data-roll-type="damage" data-damage="{{damage}}" data-monitor="{{monitor}}" data-targetid="{{speaker.token}}" data-actorId="{{speaker.actor}}" data-sceneId="{{speaker.scene}}">
                      {{#if damageApplied}}
                        <span class="damage-apply-text">{{localize 'shadowrun6.roll.damage_applied'}}</span><span class="damage-revert-text">{{localize 'shadowrun6.roll.damage_revert'}}</span> ( <i class="fa-solid fa-rotate-left"></i> )
                      {{else}}
                        {{localize 'shadowrun6.roll.apply_damage'}} ({{damage}}{{#if (eq monitor "stun")}}S{{else}}P{{/if}})
                      {{/if}}
                  </button>
                  {{#if damageConvertedStun}}
                  <button style="margin-top: 5px;" class="damageConvertedStun rollable{{#if damageApplied2}} damageApplied{{/if}}" data-roll-type="damage" data-damage="{{damageConvertedStun}}" data-monitor="stun" data-targetid="{{speaker.token}}" data-actorId="{{speaker.actor}}" data-sceneId="{{speaker.scene}}">
                      {{#if damageApplied2}}
                        <span class="damageConvertedStun damage-apply-text">{{localize 'shadowrun6.roll.damage_applied'}}</span><span class="damage-revert-text">{{localize 'shadowrun6.roll.damage_revert'}}</span> ( <i class="fa-solid fa-rotate-left"></i> )
                      {{else}}
                        {{localize 'shadowrun6.roll.apply_damage'}} ({{damageConvertedStun}}S)
                      {{/if}}
                  </button>
                  {{/if}}
              {{/if}}

        {{/if}}
    </div>
    {{/if}}
  {{/if}}


{{/if}}

</div>