const indexApp = angular.module('indexApp', []);
indexApp.controller('indexCtrl', ($scope) => {
  $scope.appName = 'Inverted Index';
  $scope.myInvertedIndex = new InvertedIndex();
  $scope.indexTable = '';
  $scope.indexTableFiles = [];
  $scope.currentFileName = null;
  $scope.liveSearchResult = false;
  $scope.liveSearchKeys = false;
  $scope.indexedFiles = [];

  $scope.getBookDocs = (fileName) => {
    const fileLength = $scope.myInvertedIndex.files[fileName].length;
    return Array.from({ length: fileLength }, (value, key) => key);
  };

  $scope.indexFile = () => {
    $scope.myInvertedIndex.createIndex($scope.currentFileName);
    $scope.indexTable = $scope.myInvertedIndex.indexMap;
    $scope.indexTableFiles.push($scope.currentFileName);
    $scope.indexedFiles.push($scope.currentFileName);
    $scope.currentFileName = '';
  };

  /* Validate JSON, read and store file  */
  $scope.readAndCheckFile = (selectedFile) => {
    const fileReader = new FileReader();
    fileReader.onload = () => {
      const fileJSONContent = JSON.parse(fileReader.result);
      if (!$scope.myInvertedIndex.isValidJSON(fileJSONContent)) {
        Materialize.toast('Invalid JSON file', 2000, 'red');
      } else {
        $scope.myInvertedIndex.storeFile(selectedFile.name, fileJSONContent);
        $scope.$apply(() => {
          $scope.currentFileName = selectedFile.name;
          $scope.indexTableFiles.push($scope.currentFileName);
        });
      }
    };

    fileReader.readAsText(selectedFile);
  };


  /* Handling file upload and extension checking */
  const fileInputField = document.getElementById('fileInput');
  fileInputField.addEventListener('change', (e) => {
    const selectedFile = e.target.files[0];
    const acceptedFileType = 'application/json';
    const selectedFileType = selectedFile.type;
    const isValidFileType = Object.is(selectedFileType, acceptedFileType);
    if (!isValidFileType) {
      Materialize.toast('Invalid File Type', 2000, 'cyan');
      $('#fileInput').val('');
    } else {
      $scope.readAndCheckFile(selectedFile);
    }
  });

  $('#search').on('keyup', () => {
    if (Object.keys($scope.myInvertedIndex.indexMap).length === 0) {
      Materialize.toast('Index a file first', 500, 'orange');
    }
    const searchTerm = $('#search').val();
    $scope.myInvertedIndex.searchIndex(searchTerm);
    $scope.$apply(() => {
      $scope.liveSearchResult =
        $scope.myInvertedIndex.searchIndex(searchTerm);
      $scope.liveSearchKeys = Object.keys($scope.liveSearchResult);
    });
  });
});

$(document).ready(() => {
  $('.modal').modal();
});

@@ -1,12 +1,33 @@
+let searchThis = %5B%5D;%0A
 const indexA
@@ -381,16 +381,644 @@
 s = %5B%5D;%0A
+  $scope.searchInFiles = %5B%5D;%0A  $scope.updateFilesToSearch = (e) =%3E %7B%0A    const state = e.target.checked;%0A    const value = e.target.value;%0A    console.log(value, state);%0A%0A    if ($scope.searchInFiles.indexOf(value) === -1) %7B%0A      if (state) %7B%0A        $scope.searchInFiles.push(value);%0A      %7D%0A    %7D else %7B%0A      const deleteIndex = $scope.searchInFiles.indexOf(value);%0A      $scope.searchInFiles.splice(deleteIndex, 1);%0A    %7D%0A    // Because we need it realtime%0A    const searchInputValue = document.getElementById('search').value;%0A    $scopeliveSearchResult = %0A      $scope.myInvertedIndex.searchIndex(searchInputValue);%0A  %7D;%0A%0A
 %0A  $scop
@@ -1338,19 +1338,21 @@
 ex.index
-Map
+Table
 ;%0A    $s
@@ -1495,16 +1495,17 @@
 ;%0A  %7D;%0A%0A
+%0A
   /* Val
@@ -1674,27 +1674,27 @@
  const fileJ
-SON
+son
 Content = JS
@@ -1733,38 +1733,21 @@
    if (!
-$scope.myInvertedIndex
+Utils
 .isValid
@@ -1751,20 +1751,20 @@
 lidJ
-SON
+son
 (fileJ
-SON
+son
 Cont
@@ -1882,18 +1882,14 @@
 dex.
-storeFile(
+files%5B
 sele
@@ -1905,26 +1905,27 @@
 name
-,
+%5D =
  fileJ
-SON
+son
 Content
-)
 ;%0A  
@@ -2141,24 +2141,26 @@
 le);%0A  %7D;%0A%0A%0A
+%0A%0A
   /* Handlin
@@ -2808,11 +2808,13 @@
 ndex
-Map
+Table
 ).le
@@ -2946,169 +2946,597 @@
     
-$scope.myInvertedIndex.searchIndex(searchTerm);%0A    $scope.$apply(() =%3E %7B%0A      $scope.liveSearchResult =%0A        $scope.myInvertedIndex.searchIndex(searchTerm);
+if ($scope.searchInFiles.length %3E 0) %7B%0A      $scope.myInvertedIndex.searchIndex(searchTerm, $scope.searchInFiles);%0A    %7D else %7B%0A      $scope.myInvertedIndex.searchIndex(searchTerm);%0A    %7D%0A    $scope.$apply(() =%3E %7B%0A      if ($scope.searchInFiles.length %3E 0) %7B%0A        $scope.liveSearchResult =%0A        $scope.myInvertedIndex.searchIndex(searchTerm, $scope.searchInFiles);%0A        $scope.liveSearchKeys = Object.keys($scope.liveSearchResult);%0A      %7D else %7B%0A        $scope.myInvertedIndex.searchIndex(searchTerm);%0A        $scope.liveSearchKeys = Object.keys($scope.liveSearchResult);%0A      %7D
 %0A   

modify angular controller