import { action, computed, observable, observe } from "mobx";
import browser from "webextension-polyfill";
import SuiteState from "./SuiteState";

class UiState {
  @observable selectedTest = {};
  @observable selectedCommand = null;
  @observable filterTerm = "";
  @observable dragInProgress = false;
  @observable clipboard = null;
  @observable isRecording = false;
  @observable isSelectingTarget = false;
  @observable consoleHeight = 200;
  @observable minConsoleHeight = 200;
  @observable minContentHeight = 460;

  constructor() {
    this.suiteStates = {};
    this.filterFunction = this.filterFunction.bind(this);
    browser.storage.local.get().then(storage => {
      if (storage.consoleSize && storage.consoleSize >= this.minConsoleHeight) {
        this.resizeConsole(storage.consoleSize);
      }
    });
  }

  @action.bound setProject(project) {
    this._project = project;
    observe(this._project, "id", this.projectChanged);
  }

  @computed get filteredTests() {
    return this._project.tests.filter(this.filterFunction);
  }

  @computed get baseUrl() {
    return this._project.url;
  }

  @action.bound copyToClipboard(item) {
    this.clipboard = item;
  }

  @action.bound selectTest(test, suite) {
    this.selectedTest = { test, suite };
  }

  @action.bound selectCommand(command) {
    this.selectedCommand = command;
  }

  @action.bound changeFilter(term) {
    this.filterTerm = term;
  }

  @action.bound setDrag(dragProgress) {
    this.dragInProgress = dragProgress;
  }

  @action.bound toggleRecord() {
    this.isRecording = !this.isRecording;
  }

  @action.bound setSelectingTarget(isSelecting) {
    this.isSelectingTarget = isSelecting;
  }

  @action.bound resizeConsole(height) {
    this.consoleHeight = height;
    browser.storage.local.set({
      consoleSize: height 
    });
  }

  addStateForSuite(suite) {
    this.suiteStates[suite.id] = new SuiteState(this, suite);
  }

  filterFunction({name}) {
    return (name.indexOf(this.filterTerm) !== -1);
  }

  setUrl(url, addToCache) {
    this._project.setUrl(url);
    if (addToCache) this._project.addUrl(url);
  }

  @action.bound projectChanged() {
    this.selectedTest = {};
    this.selectedCommand = null;
    this.filterTerm = "";
    this.dragInProgress = false;
    this.clipboard = null;
    this.isRecording = false;
    this.suiteStates = {};
  }
}

if (!window._state) window._state = new UiState();

export default window._state;

@@ -66,43 +66,38 @@
 ort 
-browser from %22webextension-polyfill
+storage from %22../../IO/storage
 %22;%0Ai
@@ -391,32 +391,81 @@
 Target = false;%0A
+  @observable windowHeight = window.innerHeight;%0A
   @observable co
@@ -508,34 +508,32 @@
 ConsoleHeight = 
-20
 0;%0A  @observable
@@ -665,32 +665,24 @@
 s);%0A    
-browser.
 storage.
 local.ge
@@ -673,22 +673,16 @@
 storage.
-local.
 get().th
@@ -684,23 +684,20 @@
 ().then(
-storage
+data
  =%3E %7B%0A  
@@ -704,23 +704,20 @@
     if (
-storage
+data
 .console
@@ -725,18 +725,29 @@
 ize 
-&& storage
+!== undefined && data
 .con
@@ -810,23 +810,20 @@
 Console(
-storage
+data
 .console
@@ -1794,24 +1794,16 @@
     
-browser.
 storage.
 loca
@@ -1802,14 +1802,8 @@
 age.
-local.
 set(
@@ -1844,16 +1844,95 @@
 );%0A  %7D%0A%0A
+  @action.bound setWindowHeight(height) %7B%0A    this.windowHeight = height;%0A  %7D%0A%0A
   addSta

use the storage module and recover from 0s