// DarkPlasma_Promise_SaveLayout 1.1.1
// Copyright (c) 2021 DarkPlasma

/**
 * 2021/03/13 1.1.1 顔グラを縮小表示する
 *            1.1.0 顔グラを表示する
 *            1.0.0 初版
 */

/*:ja
 * @plugindesc Promise用セーブ画面レイアウト
 * @author DarkPlasma
 * @license No License
 *
 * @target MZ
 *
 * @param faceOffsetX
 * @desc 顔グラオフセットX
 * @text 顔グラオフセットX
 * @type number
 * @default -80
 * @min -300
 *
 * @param faceOffsetY
 * @desc 顔グラオフセットY
 * @text 顔グラオフセットY
 * @type number
 * @default -120
 * @min -300
 *
 * @help
 * version: 1.1.1
 * セーブファイル数を3つに制限します。
 * セーブ・ロード画面のレイアウトをPromise用に最適化します。
 */

(() => {
  'use strict';

  const pluginName = document.currentScript.src.replace(/^.*\/(.*).js$/, function () {
    return arguments[1];
  });

  const pluginParameters = PluginManager.parameters(pluginName);

  const settings = {
    faceOffsetX: Number(pluginParameters.faceOffsetX || -80),
    faceOffsetY: Number(pluginParameters.faceOffsetY || -120),
  };

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

  Scene_File.prototype.isBottomHelpMode = function () {
    return false;
  };

  Scene_File.prototype.helpAreaHeight = function () {
    return this.calcWindowHeight(1, false);
  };

  Scene_File.prototype.buttonAreaHeight = function () {
    return 0;
  };

  Scene_File.prototype.helpWindowRect = function () {
    return new Rectangle(0, 0, Graphics.boxWidth, this.calcWindowHeight(1, false));
  };

  Scene_File.prototype.listWindowRect = function () {
    return new Rectangle(0, this.mainAreaTop(), Graphics.boxWidth, this.mainAreaHeight());
  };

  Window_SavefileList.prototype.numVisibleRows = function () {
    return 3;
  };

  Window_SavefileList.prototype.drawFace = function (faceName, faceIndex, x, y, width, height) {
    width = width || ImageManager.faceWidth;
    height = height || ImageManager.faceHeight;
    const bitmap = ImageManager.loadFace(faceName);
    const srcWidth = ImageManager.faceWidth;
    const srcHeight = ImageManager.faceHeight;
    const srcX = Math.floor((faceIndex % 4) * srcWidth);
    const srcY = Math.floor(Math.floor(faceIndex / 4) * srcHeight);
    const destWidth = Math.min(width, srcWidth);
    const destHeight = Math.min(height, srcHeight);
    const destX = Math.floor(x + Math.max(width - srcWidth, 0) / 2);
    const destY = Math.floor(y + Math.max(height - srcHeight, 0) / 2);
    this.contents.blt(bitmap, srcX, srcY, srcWidth, srcHeight, destX, destY, destWidth, destHeight);
  };

  Window_SavefileList.prototype.drawPartyCharacters = function (info, x, y) {
    if (info.faces) {
      const scale = 0.8;
      const width = Math.floor(ImageManager.faceWidth * scale);
      info.faces.forEach((face, index) => {
        this.drawFace(
          face[0],
          face[1],
          x + index * width + settings.faceOffsetX,
          y + settings.faceOffsetY,
          width,
          Math.floor(ImageManager.faceHeight * scale)
        );
      });
    }
  };
})();
