var EHDI = EHDI || Object.create(null);

EHDI.GAME = EHDI.GAME || Object.create(null);

EHDI.GAME.components = EHDI.GAME.components || Object.create(null);

EHDI.GAME.components.Card = function(data, texture) {
	this.faceUpTexture = texture;
	// EHDI.aka.Container.call(this, texture);
	EHDI.aka.Container.call(this)

	this.cframe = new EHDI.aka.Sprite(EHDI.Assets.images["pb_card_blue_frame"]);
	this.cframe.anchor.set(0.5,0.5);
	this.addChild(this.cframe);

	this.main = new EHDI.aka.Sprite(texture);
	this.main.anchor.set(0.5,0.5);
	this.addChild(this.main);

	this.foverlay = new EHDI.aka.Sprite(EHDI.Assets.images["pb_card_blue_overlay"]);
	this.foverlay.anchor.set(0.5,0.5);
	this.addChild(this.foverlay);

	this.icoleft = new EHDI.aka.Sprite();
	this.icoleft.position.set((this.main.width * -0.5) + 28, (this.main.height * -0.5) + 30);
	this.addChild(this.icoleft);

	this.icoRight = new EHDI.aka.Sprite();
	this.icoRight.position.set((this.main.width * -0.5) + 232, (this.main.height * -0.5) + 30);
	// this.addChild(this.icoRight);

	this.lblTitle = new EHDI.aka.PixiText(data.title, {
		fontFamily: 'Exo-Bold',
		fontWeight: 'bold',
		fill: 0xFFFFFF,
		fontSize : 24,
		wordWrap: true,
		wordWrapWidth: this.main.width * 0.8,
		align: "center"
	});
	this.lblTitle.anchor.set(0.5,0.5);
	this.lblTitle.position.set(0, (this.main.height * 0.20) )
	this.addChild(this.lblTitle);

	this.line = new EHDI.aka.Sprite(EHDI.Assets.images["pb_card_f_line"]);
	this.line.anchor.set(0.5,0)
	this.line.position.set(0, (this.lblTitle.y + this.lblTitle.height * 0.6));
	this.addChild(this.line);

	this.lblDesc = new EHDI.aka.PixiText(data.desc, {
		fontFamily: 'Exo-Bold',
		fontWeight: 'lighter',
		fill: 0xFFFFFF,
		fontSize : 16,
		wordWrap: true,
		wordWrapWidth: this.main.width * 0.8,
		align: "center"
	});
	this.lblDesc.anchor.set(0.5,0);
	this.lblDesc.position.set(0, this.line.y + this.line.height + 10);
	this.addChild(this.lblDesc);

	this.title = data.title;
	this.desc = data.desc;

	// this.type = null;
	var type = null;
	Object.defineProperty(this, "type", {
		set: function(val){
			type = val;
			switch (val){
				case EHDI.GAME.CardManager.CARD_TYPES.ANIMAL:
					this.icoleft.texture = EHDI.GAME.CardManager.ICO_ANIMAL;
					break;
				case EHDI.GAME.CardManager.CARD_TYPES.GROUNDLOST:
					this.icoleft.texture = EHDI.GAME.CardManager.ICO_DANGER;
					this.icoRight.texture = EHDI.GAME.CardManager.ICO_RESCUE;
					this.addChild(this.icoRight);
					break;
				case EHDI.GAME.CardManager.CARD_TYPES.PLANT:
					this.icoleft.texture = EHDI.GAME.CardManager.ICO_PLANT;
					break;
				case EHDI.GAME.CardManager.CARD_TYPES.OIL:
					this.icoleft.texture = EHDI.GAME.CardManager.ICO_ARTIFACT;
					break;
				case EHDI.GAME.CardManager.CARD_TYPES.MANWOMAN:
					this.icoleft.texture = EHDI.GAME.CardManager.ICO_MAN;
					this.icoRight.texture = EHDI.GAME.CardManager.ICO_WOMAN;
					this.addChild(this.icoRight);
					break;
				case EHDI.GAME.CardManager.CARD_TYPES.TIMECOIN:
				this.icoleft.texture = EHDI.GAME.CardManager.ICO_TIME;
				this.icoRight.texture = EHDI.GAME.CardManager.ICO_FLAME;
				this.addChild(this.icoRight);
					break;
			}
		},
		get: function(){
			return type;
		}
	});

	this.facedDown = true;
	this.effect;
	this.isAngel = false;
	this.isDrafted = false;
	this.viewMode = false;
	this.symbolMultiplier = 1,
	this.doubledSymbol,
	this.doubledEffect,
	this.scaleMax = {
		x: 1,
		y: 1
	},
	this.scaleMin = {
		x: 100 / this.main.width,
		y: 140 / this.main.height
	}
	this.pointsGained;

	this.scale.set(this.scaleMin.x, this.scaleMin.y)

	this.brightnessFilter = new PIXI.filters.ColorMatrixFilter();
	this.brightnessFilter.brightness(1.5, false);
}

EHDI.GAME.components.Card.prototype = Object.create(EHDI.aka.Container.prototype);

EHDI.GAME.components.Card.prototype.createLarge = function(){
	this.scaledCard = new EHDI.aka.Container();

	var _sprCFrame = new EHDI.aka.Sprite(this.cframe.texture);
	this.scaledCard.addChild(_sprCFrame);

	var _sprCard = new EHDI.aka.Sprite(this.faceUpTexture);
	this.scaledCard.addChild(_sprCard);

	var _sprFOverlay = new EHDI.aka.Sprite(this.foverlay.texture);
	this.scaledCard.addChild(_sprFOverlay);

	var _sprIcoL = new EHDI.aka.Sprite(this.icoleft.texture);
	_sprIcoL.position.set( 28, 30 );
	this.scaledCard.addChild(_sprIcoL);

	if(this.icoRight.texture){
		var _sprIcoR = new EHDI.aka.Sprite(this.icoRight.texture);
		_sprIcoR.position.set( 232, 30 );
		this.scaledCard.addChild(_sprIcoR);
	}

	var lblTitle = new EHDI.aka.PixiText(this.title, {
		fontFamily: 'Exo-Bold',
		fontWeight: 'bold',
		fill: 0xFFFFFF,
		fontSize : 24,
		wordWrap: true,
		wordWrapWidth: _sprCard.width * 0.8,
		align: "center"
	});
	lblTitle.anchor.set(0.5,0.5);
	lblTitle.position.set(_sprCard.width * 0.5, _sprCard.height * 0.70 )
	this.scaledCard.addChild(lblTitle);

	var line = new EHDI.aka.Sprite(EHDI.Assets.images["pb_card_f_line"]);
	line.anchor.set(0.5,0)
	line.position.set(lblTitle.x, (lblTitle.y + lblTitle.height * 0.6));
	this.scaledCard.addChild(line);

	var lblDesc = new EHDI.aka.PixiText(this.desc, {
		fontFamily: 'Exo-Bold',
		fontWeight: 'lighter',
		fill: 0xFFFFFF,
		fontSize : 16,
		wordWrap: true,
		wordWrapWidth: _sprCard.width * 0.8,
		align: "center"
	});
	lblDesc.anchor.set(0.5,0);
	lblDesc.position.set(line.x, line.y + line.height + 10);
	this.scaledCard.addChild(lblDesc);

	this.scaledCard.scaledDouble = new EHDI.aka.Sprite();
	this.scaledCard.addChild(this.scaledCard.scaledDouble);
	this.scaledCard.scaledDouble.position.set(_sprCard.width * 0.425, 50)
	this.scaledCard.pivot.set(this.scaledCard.width * 0.5, this.scaledCard.height * 0.5);
}

EHDI.GAME.components.Card.prototype.compareTypes = function(card) {
	if(this.type.val == card.type.val)
		return true;
	return false;
}

EHDI.GAME.components.Card.prototype.changeFrame = function(){
	this.cframe.texture = EHDI.GAME.CardManager.CARD_G_FRAME;
	this.foverlay.texture = EHDI.GAME.CardManager.CARD_G_OVERLAY;
}

EHDI.GAME.components.Card.prototype.setFacedDown = function() {
	var w = this.width;
	var h = this.height;

	this.facedDown = true;

	this.main.texture = EHDI.GAME.CardManager.CARD_BACK;
	this.lblTitle.visible = this.lblDesc.visible = this.line.visible =
	this.icoleft.visible = this.icoRight.visible = false;
	// EHDI.GAME.utils.scaleToFit(this, w, h);
	this.interactive = false;
	if(this.doubledSymbol)
		this.doubledSymbol.visible = false;
}

EHDI.GAME.components.Card.prototype.setFacedUp = function() {
	this.facedDown = false;
	this.lblTitle.visible = this.lblDesc.visible = this.line.visible =
	this.icoleft.visible = this.icoRight.visible = true;
	this.toggleScaled(false);
	if(this.doubledSymbol)
		this.doubledSymbol.visible = true;
}

EHDI.GAME.components.Card.prototype.swapTextures = function (card) {
	var temp;

	if(!card.facedDown)
		card.texture = this.faceUpTexture;
	if(!this.facedDown)
		this.texture = card.faceUpTexture;

	temp = this.faceUpTexture;
	this.faceUpTexture = card.faceUpTexture;
	card.faceUpTexture = temp;
}

EHDI.GAME.components.Card.prototype.checkCollision = function(obj) {
	var combinedHalfWidths, combinedHalfHeights, vx, vy;

	if(obj == null)
		return false;

	this.halfWidth = this.width / 2;
	this.halfHeight = this.height / 2;
	obj.halfWidth = obj.width / 2;
	obj.halfHeight = obj.height / 2;

	vx = this.x - obj.x;
	vy = this.y - obj.y;

	combinedHalfWidths = this.halfWidth + obj.halfWidth;
	combinedHalfHeights = this.halfHeight + obj.halfHeight;

	if (Math.abs(vx) < combinedHalfWidths)
		if (Math.abs(vy) < combinedHalfHeights)
			return true;

	return false;
};

EHDI.GAME.components.Card.prototype.setDoubledSymbol = function(sprite) {
	sprite.rotation = 0.2;
	if(this.doubledSymbol) {
		// TweenLite.to(this.doubledSymbol, 0.2, {alpha: 0, onComplete: this.removeChild.bind(this, this.doubledSymbol)})
		TweenMax.to(this.doubledSymbol, 0.2, {alpha: 0, onComplete: this.removeChild.bind(this, this.doubledSymbol)})
	}

	this.addChild(sprite);
	// TweenLite.from(sprite, 0.4, {alpha: 0});
	TweenMax.from(sprite, 0.4, {alpha: 0});

	this.doubledSymbol = sprite;
	this.scaleLabels(true);
}

EHDI.GAME.components.Card.prototype.setDrafted = function(val) {
	this.isDrafted = val;
}

EHDI.GAME.components.Card.prototype.resetState = function() {
	// this.tint = 0xFFFFFF;
	this.filters = null;
	this.pointsGained = 0;
}

EHDI.GAME.components.Card.prototype.setViewMode = function(val) {
	this.scale.x = 1;
	this.scale.y = 1;
	this.toggleScaled(val);

	this.viewMode = val;
}

EHDI.GAME.components.Card.prototype.toggleZoom = function(tl, val, params) {
	var params = params || Object.create(null);
	var x, y;
	// var tl = tl || TweenLite;
	var tl = tl || TweenMax;
	var duration = params.duration || 0.2;

	if(val) {
		// x = params.x || this.scaleMax.x + 0.3;
		// y = params.y || this.scaleMax.y + 0.3;
		x = params.x || this.scaleMin.x + 0.3;
		y = params.y || this.scaleMin.y + 0.3;
	} else {
		// x = params.x || this.scaleMax.x;
		// y = params.y || this.scaleMax.y;
		x = params.x || this.scaleMin.x;
		y = params.y || this.scaleMin.y;
	}
	tl.to(this.scale, duration, {x: x, y: y,
		onStart: EHDI.GAME.soundManager.playSFX, onStartParams: ["card_select"],});
}

EHDI.GAME.components.Card.prototype.toggleScaled = function(val) {
	this.main.texture = this.faceUpTexture;
	if(val) {
		// this.scaleMax.x = 0.3333;
		// this.scaleMax.y = 0.3333;
	} else {
		// this.scaleMax.x = 1;
		// this.scaleMax.y = 1;
		//this.scale.set(1,1);
	}
	this.scale.set(this.scaleMin.x, this.scaleMin.y);
	this.scaleLabels(val);
}

EHDI.GAME.components.Card.prototype.scaleLabels = function(val) {
	if(!this.doubledSymbol)
		return;

	//if(val) {
		this.doubledSymbol.scale.set(1, 1);
		this.doubledSymbol.position.set(-27, -150);
	/*} else {
		this.doubledSymbol.scale.set(0.3333, 0.3333);
		this.doubledSymbol.position.set(-10, -50);
	}*/
}

EHDI.GAME.components.Card.prototype.addFilter = function(filter) {
	if(this.filters)
		this.filters.push(filter);
	else
		this.filters = [filter];
}

EHDI.GAME.components.Card.prototype.toggleBrightness = function(val) {
	if(val)
		this.filters = [this.brightnessFilter];
	else
		this.filters = null;
}

EHDI.GAME.components.Card.prototype.destroy = function(){
	delete this.type;
	delete this.facedDown;
	delete this.effect;
	delete this.isAngel;
	delete this.isDrafted;
	delete this.viewMode;
	delete this.symbolMultiplier;
	delete this.doubledEffect;
	delete this.pointsGained;
	delete this.title;
	delete this.desc;
	if(this.doubledSymbol instanceof EHDI.aka.Container) this.doubledSymbol.destroy({children: true});
	delete this.doubledSymbol;
	this.scaledCard.scaledDouble.destroy({children: true});
	this.scaledCard.destroy({children: true});
	delete this.scaledCard.scaledDouble;
	delete this.scaledCard;
	EHDI.aka.Sprite.prototype.destroy.apply(this, arguments);
}