'use strict';

import {Store} from './Store.js';

import AppDispatcher from '../dispatcher/AppDispatcher';
import EventEmitter from 'eventemitter3';

import CtrlConstants from '../constants/ControlConstants.js';
import VolumeConstants from '../constants/VolumeConstants.js';


var defaultVolume = 0.75;
var defaultVolumeMute = false;

function setVolume(v) {
  localStorage.setItem("volume", v);
}

function volume() {
  var v = localStorage.getItem("volume");
  if (v === null) {
    setVolume(defaultVolume);
    return defaultVolume;
  }
  return parseFloat(v);
}

function setVolumeMute(v) {
  localStorage.setItem("volumeMute", v);
}

function volumeMute() {
  var v = localStorage.getItem("volumeMute");
  if (v === null) {
    setVolumeMute(defaultVolumeMute);
    return defaultVolumeMute;
  }
  return (v === "true");
}


class VolumeStore extends Store {
  getVolume() {
    return volumeMute() ? 0.0 : volume();
  }

  getVolumeMute() {
    return volumeMute();
  }
}

var _volumeStore = new VolumeStore();

_volumeStore.dispatchToken = AppDispatcher.register(function(payload) {
  var action = payload.action;
  var source = payload.source;

  if (source === 'SERVER_ACTION') {
    if (action.actionType === CtrlConstants.CTRL) {
      switch (action.data) {

        case CtrlConstants.TOGGLE_MUTE:
          setVolumeMute(!volumeMute());
          _volumeStore.emitChange();
          break;

        default:
          break;
      }

      if (action.data.Key) {
        switch (action.data.Key) {
          case "volume":
            setVolume(action.data.Value);
            if (action.data.Value > 0) {
              setVolumeMute(false);
            }
            _volumeStore.emitChange();
            break;

          case "mute":
            setVolumeMute(action.data.Value);
            _volumeStore.emitChange();
            break;

          default:
            break;
        }
      }
    }
  }

  if (source === 'VIEW_ACTION') {
    switch (action.actionType) {

      case VolumeConstants.SET_VOLUME:
        setVolume(action.volume);
        if (action.volume > 0) {
          setVolumeMute(false);
        }
        _volumeStore.emitChange();
        break;

      case VolumeConstants.SET_VOLUME_MUTE:
        setVolumeMute(action.volumeMute);
        _volumeStore.emitChange();
        break;

      case VolumeConstants.TOGGLE_VOLUME_MUTE:
        setVolumeMute(!volumeMute());
        _volumeStore.emitChange();
        break;

      default:
        break;
    }
  }

  return true;
});

export default _volumeStore;

@@ -329,32 +329,33 @@
 alse;%0A%0Afunction 
+_
 setVolume(v) %7B%0A 
@@ -349,24 +349,24 @@
 Volume(v) %7B%0A
-
   localStora
@@ -824,16 +824,105 @@
 e%22);%0A%7D%0A%0A
+function setVolume(v) %7B%0A  _setVolume(v);%0A  if (v %3E 0) %7B%0A    setVolumeMute(false);%0A  %7D%0A%7D%0A%0A
 %0Aclass V
@@ -1668,99 +1668,8 @@
 e);%0A
-            if (action.data.Value %3E 0) %7B%0A              setVolumeMute(false);%0A            %7D%0A
     
@@ -2025,16 +2025,16 @@
 VOLUME:%0A
+
         
@@ -2063,83 +2063,8 @@
 e);%0A
-        if (action.volume %3E 0) %7B%0A          setVolumeMute(false);%0A        %7D%0A
     

Refactor setVolume