#include "../../../lib/std.mi"

#define ROTATION_LAYER_VAR VolumeKnob
#include "rotationlayer.m"

#define CUSTOM_SEEK_VAR MiniSeek
#include "../../../lib/customseek.m"

#undef CUSTOM_SEEK_VAR
#define CUSTOM_SEEK_VAR MainSeek
#include "../../../lib/customseek.m"

#define SDRAG_VOLUME_VAR Volume
#include "simpledragvolume.m"


Global Map SeekMap, MainSeekMap;

Global Text Volumeticker;

Global Wac MainComp, Playlist;

Class GuiObject HintObject;
Class Button HintButton;

Global HintObject Volume, Seek, SeekGhost, MainSeek, MainSeekGhost, visual;
Global HintButton PlayPause;

System.onScriptUnloading() {
  _MiniSeekShutdown();
  _MainSeekShutdown();
}

System.onScriptLoaded() {
  // Get Main and playlist components
  MainComp = getWac("{3CBD4483-DC44-11d3-B608-000086340885}");
  Playlist = getWac("{45F3F7C1-A6F3-4EE6-A15E-125E92FC3F8D}");

  Layout mainnormal = getContainer("Main").getLayout("Normal");
  Layout mainshade = getContainer("Main").getLayout("shade");

  Volume = mainnormal.getObject("volumec");
  PlayPause = mainnormal.getObject("play");
  
  Volumeticker = mainnormal.getObject("Volumeticker");

  Seek = mainshade.getObject("seek");
  SeekGhost = mainshade.getObject("seekghost");

  MainSeek = mainnormal.getObject("mainseek");
  MainSeekGhost = mainnormal.getObject("mainseekghost");

  SeekMap = new Map;
  SeekMap.loadMap("player.winshade.seek.mask");

  MainSeekMap = new Map;
  MainSeekMap.loadMap("player.main.seek.mask");
  
  _MainSeekInit( MainSeek, MainSeekGhost, MainSeekMap);

  _MiniSeekInit( Seek, SeekGhost, SeekMap);

  _VolumeKnobInit(mainnormal, "volumec"); // init rotationlayer

  _VolumeInit(Volume);
  _VolumeSetMaxDistance(200);

Region r = new Region;
r.loadFromBitmap("player.main.volume.bg");
Layer Knob = mainnormal.getObject("volumec");
Knob.setRegion(r);
delete r;

  _VolumeKnobRotateDegree(-(getVolume()*360/255));
  Volumeticker.setText(integerToString( getVolume()*100/255 )  + "%");
}

PlayPause.onLeftClick() {
  int IsPlayingInt;
  IsPlayingInt = System.getLeftVuMeter();

  if (IsPlayingInt != NULL) {
    System.Pause();
  }
  else {
    System.Play();
  }
}

/* volume stuff */

System.onVolumeChanged(int newvol) {
  _VolumeKnobRotateDegree(-(newvol*360/255));
  Volumeticker.setText(integerToString(getVolume()*100/255 )  + "%");
}

