//=============================================================================
// MrTS_SimpleSaveLoadMenu.js
//=============================================================================

/*:
* @plugindesc Shows faces instead of character sprites and adds 4 lines of things.
* @author Mr. Trivel
*
* @param Background Image
* @desc Background image name for saving and loading. Leave blank for default.
* Default: 
* @default 
*
* @param Select Text
* @desc Select savefile text.
* Default: Select Savefile
* @default Select Savelife
*
* @param Line 1
* @desc What to display in line 1? 'gold' 'time' 'variable [ID]' 'none'
* Default: time
* @default time
*
* @param Line 2
* @desc What to display in line 2? 'gold' 'time' 'variable [ID]' 'none'
* Default: gold
* @default gold
*
* @param Line 3
* @desc What to display in line 3? 'gold' 'time' 'variable [ID]' 'none'
* Default: none
* @default none
*
* @param Line 1 Icon
* @desc Which icon to display before Line 1?
* Default 220 
* @default 220
*
* @param Line 2 Icon
* @desc Which icon to display before Line 2?
* Default: 313
* @default 313
*
* @param Line 3 Icon
* @desc Which icon to display before Line 3?
* Default: 0
* @default 0
*
* @param Max Saves
* @desc Amount of Savefiles available.
* Default: 99
* @default 99
* 
* @help 
* --------------------------------------------------------------------------------
* Terms of Use
* --------------------------------------------------------------------------------
* Don't remove the header or claim that you wrote this plugin.
* Credit Mr. Trivel if using this plugin in your project.
* Free for commercial and for non-commercial projects.
* --------------------------------------------------------------------------------
* Version 1.0
* --------------------------------------------------------------------------------
*
* Everything is in plugin parameters.
*
* --------------------------------------------------------------------------------
* Version History
* --------------------------------------------------------------------------------
* 1.0 - Release
*/

(function() {
	var parameters = PluginManager.parameters('MrTS_SimpleSaveLoadMenu');

	var paramBackgroundImage = String(parameters['Background Image'] || "");
	var paramSelectText = String("Select Savefile");
	var paramLine1 = String(parameters['Line 1'] || "time");
	var paramLine2 = String(parameters['Line 2'] || "gold");
	var paramLine3 = String(parameters['Line 3'] || "none");
	var paramLine1Icon = Number(parameters['Line 1 Icon'] || 220);
	var paramLine2Icon = Number(parameters['Line 2 Icon'] || 313);
	var paramLine3Icon = Number(parameters['Line 3 Icon'] || 0);
	var paramLineIcons = [paramLine1Icon, paramLine2Icon, paramLine3Icon];
	var paramMaxSaves = Number(parameters['Max Saves'] || 99);

	DataManager.maxSavefiles = function() {
	    return paramMaxSaves;
	};


	Scene_File.prototype.createBackground = function() {
	    this._backgroundSprite = new Sprite();
	    this._backgroundSprite.bitmap = paramBackgroundImage.length > 0 ? ImageManager.loadSystem(paramBackgroundImage) : SceneManager.backgroundBitmap();
	    this.addChild(this._backgroundSprite);
	};

	var _SceneFile_createListWindow = Scene_File.prototype.createListWindow;
	Scene_File.prototype.createListWindow = function() {
		_SceneFile_createListWindow.call(this);
		var rectH = this._listWindow.itemRect(0).height;
	    var y = this._helpWindow.height;
	    var occupiedSpace = this._listWindow.standardPadding() * 2 + y;
	    var possibleItems = 3;
	    var height = possibleItems * rectH + this._listWindow.standardPadding() * 2;
	    var width = 916;
	    this._listWindow.width = width;
	    this._listWindow.height = height;
	    this._listWindow.refresh();
	    this._listWindow.y = y + (Graphics.boxHeight-y)/2 - height/2;
	    this._listWindow.x = Graphics.boxWidth/2 - width/2;

	    this.addWindow(this._listWindow);

	    this._helpWindow.width = width;
		this._helpWindow.opacity = 0;
	    this._helpWindow.x = this._listWindow.x;
	    this._helpWindow.y = this._listWindow.y/2 - this._helpWindow.height/2;
	};

	Window_SavefileList.prototype.drawItem = function(index) {
	    var id = index + 1;
	    var valid = DataManager.isThisGameFile(id);
	    var info = DataManager.loadSavefileInfo(id);
	    var rect = this.itemRectForText(index);
	    this.resetTextColor();
	    if (this._mode === 'load') {
	        this.changePaintOpacity(valid);
	    }
	    if (info) {
	        this.changePaintOpacity(valid);
	        this.drawContents(info, rect, valid);
	        this.changePaintOpacity(true);
	    }
	};

	Window_SavefileList.prototype.drawContents = function(info, rect, valid) {
	    var bottom = rect.y + rect.height;
	    if (rect.width >= 420) {
			this.drawGameTitle(info, rect.x, rect.y +8, rect.width - 192);
	        if (valid) {
	            this.drawPartyCharacters(info, rect.x + 40, bottom - 12);
	        }
	    }
	    var lineHeight = this.lineHeight();
	    var y2 = rect.y + this.padding/2;
	    for (var i = 0; i < paramLineIcons.length; i++) {
	    	this.drawLine(info, rect.x, y2, rect.width, i+1);
	    	y2 += lineHeight;
	    }
	};
	
	Window_SavefileList.prototype.drawGameTitle = function(info, x, y, width) {
        if (info.title) {
        this.drawText(info.title, x, y, width);
		}
	};

	Window_SavefileList.prototype.drawPartyCharacters = function(info, x, y) {
		if (info.characters) {
        for (var i = 0; i < info.characters.length; i++) {
            var data = info.characters[i];
            this.drawCharacter(data[0], data[1], x + i * 48, y);
			}
		}
	};

	Window_SavefileList.prototype.drawLine = function(info, x, y, width, line) {
		var txt = info["line"+line];
		this.drawText(txt, x, y, width, 'right');
		this.drawIcon(paramLineIcons[line-1], x + width - this.textWidth(txt)-Window_Base._iconWidth-4, y);	
	};

	Window_SavefileList.prototype.itemHeight = function() {
		return 120;
	};

	var _DataManager_makeSavefileInfo = DataManager.makeSavefileInfo;
	DataManager.makeSavefileInfo = function() {
		var info = _DataManager_makeSavefileInfo();
		var data = [paramLine1, paramLine2, paramLine3];
		var parsedData = [];
		for (var i = 0; i < data.length; i++) {
			switch (data[i].split(' ')[0])
			{
				case 'gold':
				{
					parsedData[i] = $gameParty.gold();
				} break;
				case 'time':
				{
					parsedData[i] = $gameSystem.playtimeText();
				} break;
				case 'variable':
				{
					parsedData[i] = $gameVariables.value(Number(data[i].split(' ')[1]));
				} break;
				default:
				{
					parsedData[i] = '';
				} break;
				
			}
		}
		info.line1 = parsedData[0];
		info.line2 = parsedData[1];
		info.line3 = parsedData[2];
		console.log(info);
	    return info;
	};
})();