// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you 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.

import browser from "webextension-polyfill";
import parser from "ua-parser-js";
import { migrateTestCase, migrateProject } from "./legacy/migrate";
import UiState from "../stores/view/UiState";
import ModalState from "../stores/view/ModalState";
import Selianize, { ParseError } from "../../selianize";
import chromeGetFile from "./filesystem/chrome";
import firefoxGetFile from "./filesystem/firefox";

export const supportedFileFormats = ".side, text/html, application/zip";
const parsedUA = parser(window.navigator.userAgent);

export function getFile(path) {
  const browserName = parsedUA.browser.name;
  if (browserName === "Chrome") {
    return chromeGetFile(path);
  } else if (browserName === "Firefox") {
    return firefoxGetFile(path);
  } else {
    return Promise.reject(new Error("Operation is not supported in this browser"));
  }
}

export function saveProject(project) {
  project.version = "1.0";
  downloadProject(project);
  UiState.saved();
}

function downloadProject(project) {
  browser.downloads.download({
    filename: project.name + ".side",
    url: createBlob("application/json", project.toJSON()),
    saveAs: true,
    conflictAction: "overwrite"
  });
}

export function exportProject(project) {
  Selianize(JSON.parse(project.toJSON())).then(data => {
    browser.downloads.download({
      filename: project.name + ".test.js",
      url: createBlob("application/javascript", data),
      saveAs: true,
      conflictAction: "overwrite"
    });
  }).catch(err => {
    const markdown = ParseError(err && err.message || err);
    ModalState.showAlert({
      title: "Error exporting project",
      description: markdown,
      confirmLabel: "Download log",
      cancelLabel: "Close"
    }, (choseDownload) => {
      if (choseDownload) {
        browser.downloads.download({
          filename: project.name + "-logs.md",
          url: createBlob("text/markdown", markdown),
          saveAs: true,
          conflictAction: "overwrite"
        });
      }
    });
  });
}

let previousFile = null;
function createBlob(mimeType, data) {
  const blob = new Blob([data], {
    type: "text/plain"
  });
  // If we are replacing a previously generated file we need to
  // manually revoke the object URL to avoid memory leaks.
  if (previousFile !== null) {
    window.URL.revokeObjectURL(previousFile);
  }
  previousFile = window.URL.createObjectURL(blob);
  return previousFile;
}

export function loadProject(project, file) {
  function displayError(error) {
    ModalState.showAlert({
      title: "Error migrating project",
      description: error.message,
      confirmLabel: "Close"
    });
  }
  const fileReader = new FileReader();
  fileReader.onload = (e) => {
    if (/\.side$/.test(file.name)) {
      loadJSONProject(project, e.target.result);
    } else  {
      try {
        if (file.type === "text/html") {
          project.fromJS(migrateTestCase(e.target.result));
        } else if (file.type === "application/zip") {
          migrateProject(e.target.result).then(jsRep => {
            project.fromJS(jsRep);
          }).catch(displayError);
        }
      } catch (error) {
        displayError(error);
      }
    }
  };

  file.type === "application/zip" ? fileReader.readAsArrayBuffer(file) : fileReader.readAsText(file);
}

function loadJSONProject(project, data) {
  project.fromJS(JSON.parse(data));
}

@@ -1405,16 +1405,36 @@
 r.name;%0A
+  return (() =%3E %7B%0A  
   if (br
@@ -1459,24 +1459,26 @@
 ome%22) %7B%0A    
+ 
+ 
 return chrom
@@ -1485,32 +1485,34 @@
 eGetFile(path);%0A
+  
   %7D else if (bro
@@ -1537,24 +1537,26 @@
 fox%22) %7B%0A    
+ 
+ 
 return firef
@@ -1572,16 +1572,18 @@
 (path);%0A
+  
   %7D else
@@ -1585,16 +1585,18 @@
  else %7B%0A
+  
     retu
@@ -1673,17 +1673,246 @@
 r%22));%0A  
-%7D
+  %7D%0A  %7D)().then(blob =%3E %7B%0A    return new Promise((res) =%3E %7B%0A      const reader = new FileReader();%0A      reader.addEventListener(%22load%22, () =%3E %7B%0A        res(reader.result);%0A      %7D);%0A      reader.readAsDataURL(blob);%0A    %7D);%0A  %7D);
 %0A%7D%0A%0Aexpo

read as base64