var _ = require('lodash')
var PIXI = require('pixi.js')

var classFactory = require('carch/util/class-factory')

var MinionView = require('carch/browser/minion-view')

module.exports = classFactory(function HideoutView(proto){
  proto.init = function(options){
    options = options || {}

    this.tickManager = options.tickManager
    this.hideout = options.hideout
    this.pixiContainer = this.initContainer()
    options.pixiContainer.addChild(this.pixiContainer)

    var self = this
    this.hideout.on('addMinion', function(hideout, minion, coord){
      self.addMinionView(minion, coord)
    })
    for(var i = 0; i < this.hideout.minions.length; i++){
      var minion = this.hideout.minions[i]
      self.addMinionView(minion, this.hideout.coordOfMinion[minion])
    }
  }

  proto.initContainer = function(){
    var texture = PIXI.Texture.fromImage('tile.png')
    var drawWidth = this.hideout.width * 64
    var drawHeight = this.hideout.height * 64
    var container = new PIXI.TilingSprite(texture, drawWidth, drawHeight)
    container.interactive = true
    // Bug - need to manually set hitArea of TilingSprite:
    //   https://github.com/GoodBoyDigital/pixi.js/issues/1132
    container.hitArea = new PIXI.Rectangle(0, 0, drawWidth, drawHeight)

    var moveToPoint = new PIXI.Point()
    function moving(moveData){
      moveData.getLocalPosition(container, moveToPoint)
      console.log(moveToPoint)
      window.moveData = moveData
    }
    container.mousedown = container.touchstart = function(startData){
      container.mousemove = moving
    }

    container.mouseup = container.touchend = container.mouseupoutside = container.touchendoutside = function(stopData){
      delete container.mousemove
    }

    return container
  }

  proto.addMinionView = function(minion, coord){
    var minionView = MinionView.create({
      displayCoord: _.bind(this.displayCoord, this),
      tickManager: this.tickManager,
      minion: minion,
      coord: coord,
    })
    this.pixiContainer.addChild(minionView.sprite)
  }

  proto.displayCoord = function(coord){
    return [(coord[0] + this.hideout.dirWidth) * 64,
            (coord[1] + this.hideout.dirHeight) * 64]
  }
})

@@ -1271,38 +1271,22 @@
 var 
-moveToPoint = new PIXI.
+reference
 Point
-()
 %0A   
@@ -1323,57 +1323,67 @@
     
-moveData.getLocalPosition(container, moveTo
+container.position.x = moveData.global.x - reference
 Point
-)
+.x
 %0A   
@@ -1392,69 +1392,72 @@
  con
-sole.log(moveToPoint)%0A      window.moveData = moveData
+tainer.position.y = moveData.global.y - referencePoint.y
 %0A    %7D%0A
+%0A
     
@@ -1528,31 +1528,195 @@
 %7B%0A      
-container.mouse
+referencePoint = startData.global.clone()%0A      referencePoint.x -= container.position.x%0A      referencePoint.y -= container.position.y%0A      container.mousemove = container.touch
 move = m
@@ -1881,16 +1881,49 @@
 usemove%0A
+      delete container.touchmove%0A
     %7D%0A%0A 

Move container