@*
 * Copyright 2021 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *@

@(field: Field, radioOptions: Seq[((String, String, String), String)], args: (Symbol, Any)*)(implicit messages: Messages, formInstance: Option[Form[_]] = None)

@import play.api.i18n._
@import views.html.helper._
@import views.html.helpers._
@import views.helpers._

@elements = @{new FieldElements(field.id, field, null, args.toMap, messages) }
@parentField = @{args.toMap.get('parentField).asInstanceOf[Option[Field]]}
@errors = @{ FormHelpers.getErrors(field, parentField) }
@labelAfter = @{ elements.args.get('_labelAfter).getOrElse(false).asInstanceOf[Boolean] }
@trackGA = @{ elements.args.get('_trackGA).getOrElse(false).asInstanceOf[Boolean] }
@formHeaderKey = @{ elements.args.get('_formHeaderKey).getOrElse("service.title").asInstanceOf[String] }


<fieldset
  @if(elements.args.get('_fieldsetAttributes).isDefined) {
    @elements.args.get('_fieldsetAttributes)
  }>

  @if(elements.args.get('_legend).isDefined) {

    @commonHelper(field,errors)

    <legend
      @if(elements.args.get('_legendClass).isDefined) {
        class="@elements.args.get('_legendClass)"
      }
      @if(elements.args.get('_fieldsetAttributesLegend).isDefined) {
        id="@elements.args.get('_fieldsetAttributesLegend)"
      }><h1>@elements.args.get('_legend)</h1></legend>

    @if(elements.args.get('_fieldsetQuestion).isDefined) {
      <details role="group">
        <summary role="button" aria-controls="details-content-0" tabindex="0">
          <span class="summary" id="@if(elements.args.get('_fieldsetAttributesHelpQuestion).isDefined){@elements.args.get('_fieldsetAttributesHelpQuestion)}">@elements.args.get('_fieldsetQuestion)</span>
        </summary>
        <div class="panel-indent" id="details-content-0">
          <p id="@if(elements.args.get('_fieldsetAttributesHelpQuestion).isDefined){@elements.args.get('_fieldsetAttributesHelpQuestion)}Answer">@if(elements.args.get('_HelpQuestionAnswer).isDefined) {@elements.args.get('_HelpQuestionAnswer)}</p>
        </div>
      </details>
    }

  }

  @radioOptions.map { case ((value, id, dataTarget), label) =>
  @defining(s"${elements.field.name}-${id.toLowerCase.replace(" ","_")}")  {inputId =>

    <div class="multiple-choice" @if(dataTarget != "none"){data-target="@dataTarget"}>
        <input
                type="radio"
                id="@inputId"
                name="@elements.field.name"
                value="@value"
                @if(dataTarget != "none"){aria-controls="@dataTarget"}
                @elements.args.get('_inputClass).map{inputClass => class="@inputClass"}
        @if(elements.args.contains('_dataAttributes) ){ @elements.args.get('_dataAttributes)}
        @if(trackGA){data-journey-click="radio - select:@Messages(formHeaderKey):@id"}
        @field.value.filter( _ == value).map{_ => checked="checked"}/>

        <label for="@inputId" @elements.args.get('_labelClass).map{labelClass => class="@labelClass@field.value.filter( _ == value).map{_ => selected}"}>
            @if(!labelAfter) {
              @if(elements.args.get('_stackedLabel)) {
                @if(label.split(" ").length < 2) {
                  <br>@label
                } else {
                  @for( (l, index) <- label.split(" ").zipWithIndex) {
                    @if(index != 0) {<br>}@l
                  }
                }
              } else { @label }
            }


          @if(labelAfter){@if(elements.args.get('_stackedLabel)){@if(label.split(" ").length < 2){<br>@label}else{@for((l, index) <- label.split(" ").zipWithIndex){@if(index != 0){<br>}@l}}}else{@Html(label)}}</label>
    </div>
  }
}
</fieldset>
