//=============================================================================
// MUR Window Info v2.0
// Copyright (C) 2018,2020 MUR (https://github.com/murlab)
// BSD 3-Clause "New" or "Revised" License
// Free for use with both free and commercial RPG Maker games
//=============================================================================

var Imported = Imported || {};
Imported.MUR_WINDOWINFO = true;

var MUR = MUR || {};                // MUR's main object
MUR.WINDOWINFO = MUR.WINDOWINFO || {};  // Plugins's stuff

/*:
 * @plugindesc v2.0 Window Info
 * @author MUR
 * @help Информационное окно с данными скилов и предметов
 *
*/

//------------------------------------------------------
// Создаём информационное окно для выбора навыков и заклинаний
//------------------------------------------------------
function WindowInfo() {
	this.initialize.apply(this, arguments);
	this.itemBitmap = this.defaultBitmap;
	this._costType = '';
	this._itemName = '';
	this._itemGrade = '';
	this._itemType = '';
	this._itemDis = '';
	this._itemCost = 0;
	this._weaponId1 = 0;
	this._weaponId2 = 0;
	this._damage = [];
	this._effects = [];
	this._params = [];
	this._traits = [];
	this._stats = [];
	this._statMode = false;		// режим отображения статы
	this._invMode = false;		// режим отображения инвентарь
	this._errMode = false;		// не возможно одеть / использовать
	this._errMsg = [];			// список ошибок

	this._actor = null;
	this._tempActor = null;
	this._gradesColor = {
		ng:'grey',			// crude (сырой) / серый
		d: 'white',			// basis (основной) / белый
		c: 'green',			// grand (великий) / зелёный
		b: 'blue',			// rare (раритет) / синий
		a: 'violet',		// arcane (скрытый) / фиолет
		s: 'red',			// heroic (героический) / красный
		r1:'yellow',		// epic (эпик) / желтый
		r2:'orange'			// legendary (легендарый) / оранжевый
	}
}

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

WindowInfo.prototype.initialize = function(x, y, width, height) {
	Window_Base.prototype.initialize.call(this, x, y, width, height);
	this.hide();
};

WindowInfo.prototype.setActor = function(actor) {
	if (this._actor !== actor) {
		this._actor = actor;
		this.refresh();
	}
};

WindowInfo.prototype.setTempActor = function(tempActor) {
	if (this._tempActor !== tempActor) {
		this._tempActor = tempActor;
		this.refresh();
	}
};

WindowInfo.prototype.refresh = function() {
	this.contents.clear();
	var w = this.width;
	this.contents.paintOpacity = 255;

	if (this._statMode) {
		var y = 35;
		MUR.setfont(this.contents, 'n', 'yellow');
		this.drawText(MUR.dic['equipStatMsg'], 0, y, w, 'center');
		y += 28;
		MUR.setfont(this.contents, 's', 'green');
		var dis = MUR.dic['equipStatMsgDis'];
		dis = dis.replace('%n', this._actor.name());
		dis = dis.replace('%c', this._actor.currentClass().name);
		this.drawText(dis, 0, y, w, 'center');
		y += 3;

		var pt = MUR.dic["actorParams"];
		var stepLine = 22;
		var startY = y;
		var newCol = false;
		var p = 5;

		for (var i=0; i<pt.length; i++) {
			var pVal = this._stats[i]; 
			if (isFloat(pVal)) pVal = pVal.toFixed(2);

			if (y > this.height - stepLine*2 && !newCol) {
				y = startY;
				newCol = true;
			}
			var x = 0;
			if (newCol) x = w/2+p;

			MUR.setfont(this.contents, 's', 'peach');

			var msg = pt[i].split('\n');
			for (var j=0 ; j<msg.length; j++) {	
				y += stepLine;
				this.drawText(msg[j], x, y, w/2-p, 'left');
			}

			MUR.setfont(this.contents, 's', 'yellow');
			if (pVal == 0) MUR.setfont(this.contents, 's', 'grey');

			var up = false;
			if ((i == 22 || i >=24 && i <= 26) && pVal != 0) {
				if (pVal*100 > 100) {
					MUR.setfont(this.contents, 's', 'red');
				} else {
					MUR.setfont(this.contents, 's', 'green');
					up = true;
				}

			} else if (i > 18 && pVal != 0) {
				if (pVal*100 > 100) {
					MUR.setfont(this.contents, 's', 'green');
					up = true;
				} else {
					MUR.setfont(this.contents, 's', 'red');
				}
			}

			var pMsg = pVal;
			if (i < 8) {
				pMsg = "+" + pVal + " \\f[sWhite](" + this._actor.param(i) + ")";
			} else if (i >= 8 && i < 18) {
				pMsg = '+' + (pVal*100) + "%";
			} else {
				pMsg = '*' + (pVal*100) + "%";
			}

			this.drawText(pMsg, x, y, w/2-p, 'right');
		}
	
	} else {
		var y = 40;
		if (this._invMode) {
			MUR.setfont(this.contents, 'n', 'blue');
			this.drawText(MUR.dic['invMsg'], 0, y, w, 'center');
			y += 40;
		}
		if (this._itemGrade) {
			MUR.setfont(this.contents, 'n', this._gradesColor[this._itemGrade]);
		} else {
			MUR.setfont(this.contents, 'n', 'brown');
		}

		if (this._itemName == MUR.dic['equipEmptyMsg'] && !this._invMode) {
			y = this.height/2;
		}
		
		this.drawText(this._itemName, 0, y, w, 'center');

		
		y += 30;
		MUR.setfont(this.contents, 's', 'yellow');

		if (this._itemType) {
			this.drawText(this._itemType + ', ' + MUR.dic.gradeList[this._itemGrade] + ' ' + MUR.dic['equipGradeMsg'], 0, y, w, 'center');
		}
		
		if (this._itemDis) {
			y += 25;
			MUR.setfont(this.contents, 's', 'grey');
			var [d1, d2] = this._itemDis.split('\n');
			this.drawText(d1, 0, y, w, 'center');
			y += 18;
			this.drawText(d2, 0, y, w, 'center');
		}
		
		if (this._itemCost != undefined) {
			y += 30;
			MUR.setfont(this.contents, 'm', 'red');
			this.drawText(MUR.dic["costMsg"] + " " + this._costType + ": " + this._itemCost, 0, y, w, 'center');
		}

		MUR.setfont(this.contents, 'm', 'white');

		if (this._weaponId1 && this._weaponId1 != 0) {
			y += 30;
			MUR.setfont(this.contents, 'm', 'red');
			var weaponMsg = MUR.dic["weaponRestrictionMsg"] + ": " + $dataSystem.weaponTypes[this._weaponId1];
			if (this._weaponId2 != 0) {
				weaponMsg += " " + MUR.dic["andMsg"] + " " + $dataSystem.weaponTypes[this._weaponId2];
			}
			this.drawText(weaponMsg, 0, y, w, 'center');
		}

		MUR.setfont(this.contents, 'm', 'white');

		if (this._params && this._params.length > 0) {
			for (var i=0; i<this._params.length; i++) {
				if (this._params[i] != 0) {
					var paramsMsg = MUR.dic["paramsUpMsg"][i].note;
					var paramsVal = this._params[i];
					if (paramsVal < 0) {
						paramsMsg = MUR.dic["paramsDownMsg"][i].note;
						paramsVal *= 1;
					}
					y += 30;
					var pMsg = paramsMsg.replace('%s', paramsVal);
					this.drawText(pMsg, 0, y, w, 'center');
				}
			}
		}

		if (this._traits && this._traits.length > 0) {
			for (var i=0; i<this._traits.length; i++) {
				var trait = this._traits[i];
				var traitData = MUR.traitsTypes[trait.code];
				if (traitData && traitData.length>0) {
					var tData = eval(traitData[1]);
					var traitMsg = "";
					var traitVal = "";
					if (traitData[0] != "") {
						traitMsg = traitData[0];
						if (tData[trait.dataId]) {
							traitVal = tData[trait.dataId];
						}
					} else {
						if (tData[trait.dataId]) {                                
							traitMsg = tData[trait.dataId].note;
							if (trait.value != undefined) {
								traitVal = Math.round(trait.value*100);
							}
						}
					}
					if (traitMsg != undefined) {
						var tMsg = traitMsg.replace('%s', traitVal);
						y += 30;
						this.drawText(tMsg, 0, y, w, 'center');
					}
				}
			}
		}

		if (this._damage && this._damage.type != 0) {
			var dMsg = MUR.dic["damageMsg"];
			if (this._damage.elementId > 0) {
				dMsg += " " + $dataSystem.elements[this._damage.elementId];
			} else {
				dMsg += " " + MUR.dic["damageNormalMsg"];
			}
			dMsg += "(" + MUR.dic["damageType"][this._damage.type] + ")";
			y += 30;
			this.drawText(dMsg, 0, y, w, 'center');

			dMsg = MUR.dic["damagePercentMsg"] + " ~" + this._damage.variance + "%";
			if (this._damage.critical) {
				dMsg += "(+" + MUR.dic["critMsg"] + ")";
			}
			y += 30;
			this.drawText(dMsg, 0, y, w, 'center');
		}

		if (this._effects && this._effects.length > 0) {
			y += 50;
			MUR.setfont(this.contents, 'n', 'yellow');
			this.drawText(MUR.dic["effectsMsg"], 0, y, w, 'center');

			y += 30;
			MUR.setfont(this.contents, 'm', 'white');

			for (var i=0; i<this._effects.length; i++) {
				var effectsId = this._effects[i].code;
				var effectsDataId = this._effects[i].dataId;
				var effecstPower = MUR.effectsTypes[effectsId][3];
				if (MUR.effectsTypes[effectsId][2] == 1) {
					effecstPower += this._effects[i].value1;
				} else if (MUR.effectsTypes[effectsId][2] == 100 && this._effects[i].value1 > 0) {
					effecstPower += (this._effects[i].value1 * 100) + "%";
				}
				if (this._effects[i].value2 > 0) {
					if (this._effects[i].value1 > 0) { effecstPower += "+"; }
					effecstPower += this._effects[i].value2;
				} else if (this._effects[i].value2 < 0) {
					effecstPower += this._effects[i].value2;
				}

				var effectsType = MUR.effectsTypes[effectsId][0];
				var effectsTypesData = eval (MUR.effectsTypes[effectsId][1]);

				var effectsName = "";
				var effectsNote = "";
				if (effectsTypesData && effectsTypesData[effectsDataId]) {
					effectsName = effectsTypesData[effectsDataId].name;
					effectsNote = effectsTypesData[effectsDataId].note;
				}
				if (MUR.effectsTypes[effectsId]) {
					if (i != 0 && effectsLine != "" && (effectsType != "" || effectsTypesData != null)) { effectsLine += ", "; }
					if (effectsType != "") {
						effectsLine += effectsType;
					}
					if (effectsNote && effectsNote != "") {
						effectsNote = effectsNote.replace('%s', effecstPower);
						effectsLine += effectsNote;

					} else if (effectsName && effectsName != "") {
						effectsLine += " «" + effectsName + "»";
						y += 30;
						this.drawText(effectsLine, 0, y, w, 'center');
					}                    
				}
			}
		}
		if (this._errMode) {
			y += 40;
			MUR.setfont(this.contents, 'n', 'red');
			this.drawText(MUR.dic['equipReqMsg'], 0, y, w, 'center');
			y += 35;

			MUR.setfont(this.contents, 's', 'red');

			for (var i=0; i < this._errMsg.length; i++) {
				this.drawText(this._errMsg[i], 0, y, w, 'center');
				y += 24;
			}

		} else if (this._invMode && this._actor && this._tempActor) {
			y += 40;
			MUR.setfont(this.contents, 'n', 'blue');
			this.drawText(MUR.dic['statChangeMsg'], 0, y, w, 'center');
			y += 33;
			MUR.setfont(this.contents, 's', 'white');
			for (var i = 0; i < 6; i++) {
				var paramId = 2 + i;
				var paramName = TextManager.param(paramId);
				var paramVal = this._actor.param(paramId);
				var paramValNew = this._tempActor.param(paramId);
				
				MUR.setfont(this.contents, 's', 'yellow');
				this.drawText(paramName + ':', 48, y, w/2, 'right');
				
				MUR.setfont(this.contents, 's', 'white');
				this.drawText(paramVal, w/2 + 10 + 48, y, 36, 'right');

				if (paramVal < paramValNew) {
					MUR.setfont(this.contents, 's', 'green');
				} else if (paramVal > paramValNew) {
					MUR.setfont(this.contents, 's', 'red');
				}

				this.drawText('→', w/2 + 52 + 48, y, w/2, 'left');
				this.drawText(paramValNew, w/2 + 70 + 48, y, w/2, 'left');
				y += 20;
			}
		}
	}
};

WindowInfo.prototype.spacing = function() { return 0; };
WindowInfo.prototype.standardPadding = function() { return 0; };

WindowInfo.prototype._refreshFrame = function() {}
WindowInfo.prototype._refreshBack = function() {}
