<!--
  #%L
  thinkbig-ui-feed-manager
  %%
  Copyright (C) 2017 ThinkBig Analytics
  %%
  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.
  #L%
  -->
<div>

  <card-layout header-css="filter-header" class="centered">

    <header-section>
      <div layout="row" layout-align="center start">
        <div class="card-title">Import a Feed</div>
        <span flex></span>
        <ng-md-icon icon="file_upload" style="fill:#F08C38;padding-right:16px;" size="30"></ng-md-icon>
      </div>
    </header-section>


    <body-section>


      <div layout="column" flex class="md-padding">
        <div>
          <div class="card-sub-header">Import a Feed Archive.</div>
        </div>
        <div class="layout-padding-indent">
          <md-list flex layout-fill class="list-item-table list-condensed">

            <md-list-item>
              <div layout="row" layout-fill>
                <div flex="20" class="md-list-item-text hint">
                  Type
                </div>
                <div flex="10" class="md-list-item-text hint">
                  File type
                </div>
                <div flex="60" class="md-list-item-text hint">
                  Description
                </div>

              </div>
            </md-list-item>

            <md-list-item>
              <div layout="row" layout-fill>
                <div flex="20" class="md-list-item-text ">
                  Archive
                </div>
                <div flex="10" class="md-list-item-text ">
                  ZIP
                </div>
                <div flex="60" class="md-list-item-text ">
                  An archive contains both Nifi and Kylo Feed Data.
                  <Br/>
                  This will import into Nifi and register the feed and respective template in Kylo .
                </div>
              </div>
            </md-list-item>
          </md-list>
        </div>

        <div flex layout="column" layout-align="start" style="padding-top:15px;"
             class="layout-padding-indent">
          <upload-file upload-file-model="vm.feedFile"></upload-file>
          <ng-form name="vm.importFeedForm">
            <div layout="row">
              <div layout="column" flex>
                <div layout="row" class="feed import category">
                  <md-autocomplete flex ng-disabled="vm.model.id != null"
                                   md-input-name="category"
                                   md-selected-item="vm.category"
                                   md-search-text-change="vm.categorySearchTextChanged(vm.categorySearchText)"
                                   md-search-text="vm.categorySearchText"
                                   md-selected-item-change="vm.categorySelectedItemChange(item)"
                                   md-items="item in vm.categoriesService.querySearch(vm.categorySearchText)"
                                   md-item-text="item.name"
                                   md-min-length="0"
                                   md-floating-label="Change the Category"
                                   md-require-match="true"
                                   md-select-on-match="true"
                                   md-menu-class="autocomplete-categories-template">
                    <md-item-template>
                        <span class="item-title">
                             <ng-md-icon icon="{{item.icon}}" style="fill:{{item.iconColor}}"></ng-md-icon>
                          <span> {{item.name}} </span>
                        </span>
                      <span class="item-metadata">
                          <span class="item-metastat">
                            <strong>{{item.relatedFeeds}}</strong> Related Feeds
                          </span>
                        </span>
                    </md-item-template>
                    <md-not-found>
                      No categories matching "{{vm.categorySearchText}}" were found.
                    </md-not-found>
                  </md-autocomplete>
                </div>
                <!-- Feed Data container -->
                <!-- validation messages -->
                <div ng-if="vm.feedDataImportOption.errorMessages != null && vm.feedDataImportOption.errorMessages.length >0">
                  <div ng-repeat="message in vm.feedDataImportOption.errorMessages" layout="column">
                    <div>
                      <ng-md-icon icon="warning" size="20" class="warn"></ng-md-icon>
                      <span>{{message}}</span></div>
                  </div>
                </div>

                <div layout="row">
                  <md-input-container class="condensed-no-float md-block layout-padding-bottom" flex="50">
                    <md-checkbox ng-model="vm.feedDataImportOption.overwrite" aria-label="Overwrite if exists">
                      Overwrite Feed
                    </md-checkbox>
                    <div class="hint" style="margin-top:-10px">
                      Check to replace the feed if it already exists.
                    </div>
                  </md-input-container>

                  <md-input-container class="condensed-no-float md-block layout-padding-bottom" flex="50">
                    <md-checkbox ng-model="vm.disableFeedUponImport" aria-label="Disable upon import">
                      Disable Feed upon import
                    </md-checkbox>
                    <div class="hint" style="margin-top:-10px">
                      Check to make this feed disabled after importing
                    </div>
                  </md-input-container>

                </div>

                <!-- Template Data Container -->
                <!-- validation messages -->
                <div  ng-if="vm.templateDataImportOption.errorMessages != null && vm.templateDataImportOption.errorMessages.length >0">
                  <div ng-repeat="message in vm.templateDataImportOption.errorMessages" layout="column">
                    <div>
                      <ng-md-icon icon="warning" size="20" class="warn"></ng-md-icon>
                      <span>{{message}}</span></div>
                  </div>
                </div>

                <div ng-if="vm.nifiTemplateImportOption.errorMessages != null && vm.nifiTemplateImportOption.errorMessages.length >0">
                  <div ng-repeat="message in vm.nifiTemplateImportOption.errorMessages" layout="column">
                    <div>
                      <ng-md-icon icon="warning" size="20" class="warn"></ng-md-icon>
                      <span>{{message}}</span></div>
                  </div>
                </div>



                <div layout="row">
                  <md-input-container class="condensed-no-float md-block layout-padding-bottom" flex="50">
                    <md-checkbox ng-model="vm.templateDataImportOption.overwrite" aria-label="Overwrite if exists">
                      Replace Feed Template
                    </md-checkbox>
                    <div class="hint" style="margin-top:-10px">
                      Replace and re-register the feed template if it already exists
                    </div>
                  </md-input-container>

                  <md-input-container class="condensed-no-float md-block layout-padding-bottom" flex="50">
                    <md-checkbox ng-model="vm.reusableTemplateImportOption.overwrite" aria-label="Replace Reusable Template">
                      Replace Reusable Template
                    </md-checkbox>
                    <div class="hint" style="margin-top:-10px">
                      Replace and re-initialize the reusable template, if applicable
                    </div>
                  </md-input-container>

                </div>


                <!-- Feed properties required -->

                <div ng-if="vm.feedDataImportOption.properties && vm.feedDataImportOption.properties.length >0" flex layout="column">
                  <div class="layout-padding-bottom">The following properties need to be supplied before importing this feed</div>

                  <div ng-repeat="prop in vm.feedDataImportOption.properties" layout="column" layout-align="start start">
                    <span>Processor: {{prop.processorName}}</span>
                    <div layout="row" flex layout-fill>
                      <md-input-container>
                        <label>{{prop.propertyKey}}</label>
                        <input type="text" ng-model="prop.propertyValue" autocomplete="false" name="{{prop.inputName}}">
                      </md-input-container>
                    </div>
                  </div>
                </div>

                <!-- Data source properties required -->
                <div ng-if="vm.userDatasourcesOption.properties && vm.userDatasourcesOption.properties.length > 0" flex layout="column">
                  <div class="layout-padding-bottom">The following data sources need to be supplied before importing this feed:</div>

                  <div ng-repeat="prop in vm.userDatasourcesOption.properties" layout="column" layout-align="start start">
                    <span>Data Source: {{prop.processorName}}</span>
                    <div layout="row" flex layout-fill>
                      <md-input-container>
                        <md-select ng-model="prop.propertyValue" placeholder="Select new data source">
                          <md-option ng-value="datasource.id" ng-repeat="datasource in vm.availableDatasources">{{datasource.name}}</md-option>
                        </md-select>
                      </md-input-container>
                    </div>
                  </div>
                </div>




                  <div ng-if="vm.templateDataImportOption.properties && vm.templateDataImportOption.properties.length >0 " flex layout="column">
                    <div class="layout-padding-bottom">The following properties need to be supplied before importing this feed template</div>

                    <div ng-repeat="prop in vm.templateDataImportOption.properties" layout="column" layout-align="start start">
                      <span>{{prop.processorName}}</span>
                      <div layout="row" flex layout-fill>
                        <md-input-container>
                          <label>{{prop.propertyKey}}</label>
                          <input type="text" ng-model="prop.propertyValue" autocomplete="false" name="{{prop.inputName}}">
                        </md-input-container>
                      </div>
                    </div>
                  </div>



                  <div ng-if="vm.reusableTemplateImportOption.properties && vm.reusableTemplateImportOption.properties.length >0" flex layout="column">
                    <div class="layout-padding-bottom">The following properties need to be supplied before importing this feed template</div>

                    <div ng-repeat="prop in vm.reusableTemplateImportOption.properties" layout="column" layout-align="start start">
                      <span>{{prop.processorName}}</span>
                      <div layout="row" flex layout-fill>
                        <md-input-container>
                          <label>{{prop.propertyKey}}</label>
                          <input type="text" ng-model="prop.propertyValue" autocomplete="false" name="{{prop.inputName}}">
                        </md-input-container>
                      </div>
                    </div>
                  </div>




              </div>

            </div>

            <span style="padding-top:10px"></span>
            <div layout="row" class="layout-padding-top">
              <md-button class="md-raised md-primary" ng-click="vm.importFeed()" ng-if="vm.feedFile != null"
                         ng-disabled="(vm.uploadInProgress || vm.importFeedForm.$invalid)">
                Import Feed
              </md-button>
              <md-progress-linear flex md-mode="determinate" value="{{vm.uploadProgress}}" ng-if="vm.uploadInProgress"></md-progress-linear>
            </div>
          </ng-form>
          <div layout="column">
            <div ng-repeat="msg in vm.uploadStatusMessages" layout="row">
              <div flex>
                <span ng-if="msg.complete && msg.success">
                   <ng-md-icon icon="check" size="20" style="fill:green"></ng-md-icon>
                </span>
                <span ng-if="msg.complete && !msg.success">
                  <ng-md-icon icon="error" size="20" style="fill:red"></ng-md-icon>
                </span>
                <span>{{msg.message}}</span>
              </div>
            </div>
          </div>


          <div ng-if="vm.importResult != null" class="layout-padding-top">
            <div layout="row">
              <ng-md-icon icon="{{vm.importResultIcon}}"
                          style="fill:{{vm.importResultIconColor}}"></ng-md-icon>
              <span style="padding-left:15px;">{{vm.message}}</span>
              <span flex></span>
            </div>
            <md-list class="padding-left" ng-repeat="(severity,errors)  in vm.errorMap">
              <md-list-item class="md-2-line" ng-repeat="error in errors track by $index">
                <div class="md-list-item-text">
                  <div ng-if="error.processorName != null && error.processorName != '' "
                       style="color:grey">Processor: {{::error.processorName}}
                  </div>
                  <div class="md-warn-text">{{::error.message}}</div>
                </div>
                <md-divider ng-if="!$last"></md-divider>
              </md-list-item>
            </md-list>
          </div>
        </div>
      </div>
    </body-section>
  </card-layout>
</div>
