//=============================================================================
// O_BattleViewEnamyStatus.js
//=============================================================================
/*:ja
 * @plugindesc 戦闘中 敵HP・状態 表示
 * @author 
 *
 * @help 
 */
 
(function() {
//****** Scene_Battle
	var _Scene_Battle_prototype_createEnemyWindow=Scene_Battle.prototype.createEnemyWindow;
	Scene_Battle.prototype.createEnemyWindow=function(){
		_Scene_Battle_prototype_createEnemyWindow.call(this);
		
		var enemies = $gameTroop.members();
		if(Array.isArray(enemies) && enemies.length==1) {
			var id=0;

			this._windowEnemyStatus = new Window_enemyStatus(enemies[id]);
			this.addChild(this._windowEnemyStatus);
		}	
	};

//***** Window_enemyStatus
	function Window_enemyStatus() {
		this.initialize.apply(this, arguments);
	}

	Window_enemyStatus.prototype = Object.create(Window_Base.prototype);
	Window_enemyStatus.prototype.constructor = Window_enemyStatus;

	Window_enemyStatus.prototype.initialize = function(enemy) {
		
		Window_Base.prototype.initialize.call(this, 0, 0, 1, 1);
	
		this._fadeTime=20;
		
		this._always_show = true;
	
		//this._gauge_color_back=19;
		//this._gauge_color_1=30;//20;
		//this._gauge_color_2=31;//21;
		
		this.opacity = 0;//Windowを透明に
		this._dropSpeed = 0;
		this._visibleCounter = 0;
		//this.contentsOpacity = 0;
		
		this._requestRefresh = false;
		
		this._enemy =enemy;
		this._currentHpValue = enemy._hp;
		this._displayedValue = enemy._hp;
		this._status=this.getStatusStr();//状態変更チェック用
		
		this._img_gauge_base = ImageManager.loadSystem('sys_gauge_enemy_base');
		this._img_gauge_bar = ImageManager.loadSystem('sys_gauge_enemy_bar');
		this._icon_height=32;// =アイコンの高さ
	};

	Window_enemyStatus.prototype.update = function() {
		Window_Base.prototype.update.call(this);
		if(!this._enemy){
			return;
		}else{
			this.updateOpacity();
			this.updateWindowSize();
			this.updateHpPosition();
			this.checkStatus();
			
			if(this._requestRefresh){
				this.refresh();
			}
		}
	};

	Window_enemyStatus.prototype.updateWindowSize = function() {
		var width  = this._img_gauge_base.width + 65;
		var height = this._img_gauge_base.height + 52+this._icon_height;
		if(width != this.width || height != this.height){
			
			//モンスター画像のサイズ
			var img_enemy=ImageManager.loadEnemy($dataEnemies[this._enemy._enemyId].battlerName);
			var enemy_width=img_enemy.width;
			var enemy_height=img_enemy.height;
			
			//screenXは中心点 screenYは足元の基準点？？
			
			var padding_top=-10;
			var startY=this._enemy._screenY-enemy_height-height;
			if(padding_top>startY){
				startY=padding_top;
			}
			
			this.x=this._enemy._screenX-Math.round(width/2);
			this.y=startY;
			this.width = width;
			this.height = height;
			this.createContents();
			this._requestRefresh = true;
		}
	};

	Window_enemyStatus.prototype.updateOpacity = function() {
		if(this.isShowWindow()) {
			this.contentsOpacity += 32;//表示状態
		}else{
			this.contentsOpacity -= 32;//非表示化
		}
	};

	Window_enemyStatus.prototype.isShowWindow = function() {
		if(!this._enemy.isAppeared()) return false;
		if(this._always_show && !this._enemy.isDead()) return true;
		if(this._currentHpValue !== this._displayedValue) return true;
		if(this._enemy.isSelected()) return true;
		--this._visibleCounter;
		return this._visibleCounter > 0;
	};

	Window_enemyStatus.prototype.updateHpPosition = function() {
		if(!this._enemy) return;
		if(this._currentHpValue != this._enemy.hp) {
			this._visibleCounter = this._fadeTime;
			this._currentHpValue = this._enemy.hp;
			var difference = Math.abs(this._displayedValue - this._enemy.hp);
			this._dropSpeed = Math.ceil(difference / this._fadeTime);
		}
		
		if(this._currentHpValue != this._displayedValue) {
			if(this._displayedValue > this._currentHpValue) {
				this._displayedValue = Math.max(this._displayedValue - this._dropSpeed, this._currentHpValue);
			} else if(this._displayedValue < this._currentHpValue) {
				this._displayedValue = Math.min(this._displayedValue + this._dropSpeed, this._currentHpValue);
			}
			this._requestRefresh = true;
		}
	};
	
	Window_enemyStatus.prototype.refresh = function() {
		this.contents.clear();
		if(this._enemy){
			this._requestRefresh = false;
			
			this.drawGauge();
			this.changeTextColor(this.textColor(0));
			this.drawText(this._enemy.name(), 10, -4);
			this.resetFontSettings();
			
		}
	};

	Window_enemyStatus.prototype.drawGauge = function(){
		var gauge_padding_top=13;
		
		this.contents.blt(this._img_gauge_base
						  ,0,0
						  ,this._img_gauge_base.width,this._img_gauge_base.height
						  ,0,gauge_padding_top
						  );
		
		var drowWidth = Math.round(this._img_gauge_base.width*this._displayedValue/this._enemy.mhp);		
		this.contents.blt(this._img_gauge_bar
						  ,0,0
						  ,drowWidth,this._img_gauge_base.height
						  ,0,gauge_padding_top
						  );
		this.drawActorIcons(this._enemy,0,gauge_padding_top+this._img_gauge_base.height);

	};
	
	Window_enemyStatus.prototype.checkStatus=function(){
		var status=this.getStatusStr();
		if(this._status!=status){
			this._requestRefresh=true;
			this._status=status;
		}
	};
	Window_enemyStatus.prototype.getStatusStr = function() {
		return this._enemy._states.join(',')+','+this._enemy._buffs.join(',');
	};

})();

/*
	var _Game_System_prototype_initialize = Game_System.prototype.initialize;
	Game_System.prototype.initialize = function() {
		_Game_System_prototype_initialize.call(this);
		//this.initShowEnamyStatus();
		this._showEnamyStatus = [];
	};
	
	Game_System.prototype.initShowEnamyStatus = function() {
		this._showEnamyStatus = [];
	};
	
	Game_System.prototype.showEnamyStatus = function(id) {
		if(this._showEnamyStatus === undefined){
			this.initShowEnamyStatus();
		}
		return this._showEnamyStatus.contains(id);
	};
	
	Game_System.prototype.addEnamyStatus = function(id) {
		if(this._showEnamyStatus === undefined){
			this.initShowEnamyStatus();
		}
		if(this._showEnamyStatus.contains(id)){
			return;
		}
		this._showEnamyStatus.push(id);
	};
*/
/*
	var _Scene_Battle_prototype_createAllWindows=Scene_Battle.prototype.createAllWindows;
	Scene_Battle.prototype.createAllWindows = function(){
		_Scene_Battle_prototype_createAllWindows.call(this);
		
		
		var enemies = $gameTroop.members();
		if(Array.isArray(enemies) && enemies.length==1) {
			this._windowEnemyStatus = new Window_enemyStatus(enemies[0]);
			this.addChild(this._windowEnemyStatus);
		}
	
	};
	

/*
Window_enemyStatus.prototype.drawGauge = function(dx, dy, dw, rate, color1, color2) {
		var color3 = this.textColor(this._gauge_color_back);	
		var fillW = Math.floor(dw * rate).clamp(0, dw);
		var gaugeH = this._hpGaugeHeight;	
		var gaugeY = dy + this.lineHeight() - gaugeH - 2;
		
		color3.paintOpacity = this.translucentOpacity();
		this.contents.fillRect(dx, gaugeY, dw, gaugeH, color3);
		dx += 2;
		gaugeY += 2;
		fillW = Math.max(0, fillW - 4);
		gaugeH -= 4;
		
		this.contents.gradientFillRect(dx, gaugeY, fillW, gaugeH, color1, color2);
	};
*/

	/*
	Window_enemyStatus.prototype.drawCurrentAndMax = function(current, max, x, y, width, color1, color2) {
		if(this._show_hp_max) {
			Window_Base.prototype.drawCurrentAndMax.call(this, current, max,x, y, width, color1, color2);
		}else{
			var align = SHOW_HP_TEXT ? 'right' : 'center';
			var text = toGroup(current);
			this.changeTextColor(color1);
			this.drawText(text, x, y, width, align);
		}
	};
		function toGroup(inVal) {
	  if(typeof inVal !== 'string') { inVal = String(inVal); }
	  return inVal.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) {
		return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
	  });
	};
	*/
/*
Window_enemyStatus.prototype.refresh = function() {
		this.contents.clear();
		if(this._enemy){
			this._requestRefresh = false;
			var x=this._enemy.name().length*this.contents.fontSize;
			var y = this.contents.height - this.lineHeight();
			var width = this.contents.width;
			var color1 = this.textColor(this._gauge_color_1);
			var color2 = this.textColor(this._gauge_color_2);
			var rate = this._displayedValue / this._enemy.mhp;
			
			
			this.drawGauge(x, y, width, rate, color1, color2);
			
			
			//this.contents.fontSize=this._show_name_text_size;
			this.changeTextColor(this.textColor(0));
			this.drawText(this._enemy.name(), 10, -6);
			this.resetFontSettings();
			
			
			if(this._show_hp_value) {
			  var val = this._displayedValue
			  var max = actor.mhp;
			  var w = width;
			  var color = this.hpColor(this._enemy);
			  this.drawCurrentAndMax(val, max, x, y, w, color, this.normalColor());
			}
		}
	};
*/