/*:ja
 * @plugindesc 
 * @author 
 *
 * @param GraphicWidth
 * @text 画面サイズ
 * @desc 画面サイズをいい感じに取得できないので
 * @default 816
 * @type number
 * 
 * @command PICTURE_STAND
 * @text 立ち絵表示
 * @desc 立ち絵を表示します
 *
 * @arg id
 * @text 位置
 * @desc 
 * @default left
 * @type select
 * @option 左
 * @value left
 * @option 右
 * @value right
 * @option 真ん中
 * @value center
 * 
 * @arg name
 * @text 立ち絵画像選択
 * @desc 
 * @default 
 * @dir img/pictures/
 * @type file
 * 
 * @arg expression
 * @text 表情差分の画像選択
 * @desc 
 * @default 
 * @dir img/pictures/
 * @type file
 *
 * @command PICTURE_EXPRESSION
 * @text 表情変化
 * @desc 表情変化差分を表示します
 * 
 * @arg id
 * @text 位置
 * @desc 
 * @default none
 * @type select
 * @option 自動
 * @value none
 * @option 左
 * @value left
 * @option 右
 * @value right
 * @option 真ん中
 * @value center
 * 
 * @arg name
 * @text 画像選択
 * @desc 
 * @default 
 * @dir img/pictures/
 * @type file
 * 
 * @command PICTURE_CHANGE
 * @text ピクチャー変更
 * @desc ピクチャーの位置などをそのままに画像だけを変更します
 *
 * @arg id
 * @text 位置
 * @desc 
 * @default left
 * @type select
 * @option 左
 * @value left
 * @option 右
 * @value right
 * @option 真ん中
 * @value center
 * 
 * @arg num
 * @text ID
 * @desc 
 * @default 22
 * @type number
 * 
 * @arg name
 * @text 画像選択
 * @desc 
 * @default 
 * @dir img/pictures/
 * @type file
 * 
 * @command CREAR_STANDS
 * @text 立ち絵消去
 * @desc 立ち絵ピクチャーをまとめて消去します
 * 
 * @command PICTURE_STILL
 * @text スチル表示
 * @desc スチルを表示します
 * 
 * @arg name
 * @text 画像選択
 * @desc 
 * @default 
 * @dir img/pictures/
 * @type file
 * 
 * @arg x
 * @text X座標
 * @desc 表示するX座標です
 * @default 0
 * @type number
 * @min -2000
 * @max 2000
 * 
 * @arg y
 * @text Y座標
 * @desc 表示するY座標です
 * @default 0
 * @type number
 * @min -2000
 * @max 2000
 * 
 * @command PICTURE_ITEM
 * @text アイテム表示
 * @desc アイテムを表示します
 * 
 * @arg name
 * @text 画像選択
 * @desc 
 * @default 
 * @dir img/pictures/
 * @type file
 * 
 * @arg se
 * @text 効果音選択
 * @desc 
 * @default 
 * @dir audio/se/
 * @type file
 * 
 * @command CREAR_ITEM
 * @text アイテム消去
 * @desc アイテムを消去します
 * @help
 */
 
(() => {
	
	const script = document.currentScript;
	const param  = PluginManagerEx.createParameter(script);
	
	PluginManagerEx.registerCommand(script, 'PICTURE_STAND', function(args) {
		var id = 1;
		switch (args.id) {
			case 'left': id = 5; break;
			case 'right': id = 6; break;
			case 'center': id = 7; break;
		}
		$gameScreen.standPicture(id,args.name);
		var expression = args.expression
		if(!expression){
			var base = args.name.replace(/\d/, '');
			var expression = base + '_Normal';
		}
		$gameScreen.standPicture(id + 10, expression);
	});
	
	PluginManagerEx.registerCommand(script, 'PICTURE_EXPRESSION', function(args) {
		var id = 1;
		switch (args.id) {
			case 'left': id = 5; break;
			case 'right': id = 6; break;
			case 'center': id = 7; break;
		}
        $gameScreen.expressionPicture(id, args.name);
	});

	PluginManagerEx.registerCommand(script, 'PICTURE_CHANGE', function(args) {
		var id = 1;
		if(args.num){
			id = args.num;
		}else{
			switch (args.id) {
				case 'left': id = 5; break;
				case 'right': id = 6; break;
				case 'center': id = 7; break;
			}
		}
        $gameScreen.changePicture(id, args.name);
	});

	PluginManagerEx.registerCommand(script, 'CREAR_STANDS', function(args) {
		for(var i = 5; i < 20; i++){
			$gameScreen.erasePicture(i)
		}
	});

	PluginManagerEx.registerCommand(script, 'PICTURE_STILL', function(args) {
		const wait = 30;
		$gameScreen.showPicture(21, args.name, 0, args.x, args.y, 100, 100, 0, 0);
		$gameScreen.movePicture(21, 0, args.x, args.y, 100, 100, 255, 0, wait, 0);
		$gameMap._interpreter.wait(wait);
	});

	PluginManagerEx.registerCommand(script, 'CREAR_STILL', function(args) {
		$gameScreen.movePicture(21, 0, 0, 100, 100, 0, 0, 30);
	});

	PluginManagerEx.registerCommand(script, 'PICTURE_ITEM', function(args) {
		const wait = 30;
		const startY = 64;
		if(args.se) SoundManager.playSe(args.se);
		$gameScreen.showPicture(31, args.name, 0, 0, startY, 100, 100, 0, 0);
		$gameScreen.movePicture(31, 0, 0, 0, 100, 100, 255, 0, wait, 2);
		$gameMap._interpreter.wait(wait);
	});

	PluginManagerEx.registerCommand(script, 'CREAR_ITEM', function(args) {
		const wait = 30;
		const endY = 64;
		//$gameScreen.showPicture(31, args.name, 0, 0, 0, 100, 100, 255, 0);
		$gameScreen.movePicture(31, 0, 0, endY, 100, 100, 0, 0, wait, 1);
		$gameMap._interpreter.wait(wait);
	});
	
	const center = param.GraphicWidth / 2;
	const right = param.GraphicWidth - 284 / 2;//Graphics.boxWidth - 284 - 36 - 50 - 300;
	const left = 284 - 36;// - 50;
    
	Game_Screen.prototype.changePicture = function(pictureId, name) {
  	 	var picture = this.picture(pictureId);
  	 	if (picture) {
  			picture.change(name);
  		}
	};
	
	Game_Screen.prototype.standPicture = function(pictureId, name) {
		var realPictureId = this.realPictureId(pictureId);
    	var picture = new Game_Picture();
    	var x = 0;
		var newx;
		var y = Graphics.height / 2;
    	switch (pictureId) {
    	case 1:
    	case 4:
		case 7://真ん中
		case 17://表情
			x = center;
			newx = x;
			break;
		case 6://右
		case 16://表情
			x = right - 50;
			newx = right;
			break;
		case 5://左
		case 15://表情
			x = left + 50;
			newx = left;
			break;
		}
  	  	picture.show(name, 1, x, y, 100, 100, 0, 0);
    	this._pictures[realPictureId] = picture;
        picture.move(1, newx, y, 100, 100, 255, 0, 20);
	};

	Game_Screen.prototype.expressionPicture = function(id, name) {
		//var pictureName = this.namePicture(pictureId) + '_' + name;
		var pictureId = id;
		for(var i = 0; i < 20; i++){
			if(this.namePicture(i)){
				var pictureName = this.namePicture(i).replace(/\d/, '');
				if(name.includes(pictureName)){
					pictureId = i;
					break;
				}
			}
		}
		var x = 0;
		var y = Graphics.height / 2;
		switch (pictureId) {
			case 1:
			case 4:
			case 7://真ん中
				x = center;
				break;
			case 6://右
				x = right;
				break;
			case 5://左
				x = left;
				break;
		}
		this.showPicture(pictureId + 10, name, 1, x, y, 100, 100, 255, 0);
	};

	Game_Screen.prototype.isStill = function() {
	   return !!this.picture(21);
 };
	
	Game_Screen.prototype.namePicture = function(pictureId) {
  	 	var picture = this.picture(pictureId);
  	 	if (picture) {
  			return picture.name();
  		}
  		return '';
	};
	
	Game_Screen.prototype.xPicture = function(pictureId) {
  	 	var picture = this.picture(pictureId);
  	 	if (picture) {
  			return picture.x();
  		}
  		return false;
	};
	
	Game_Screen.prototype.yPicture = function(pictureId) {
  	 	var picture = this.picture(pictureId);
  	 	if (picture) {
  			return picture.y();
  		}
  		return false;
	};

	Game_Screen.prototype.isOpacityPicture = function(pictureId) {
		var picture = this.picture(pictureId);
		if (picture) {
		   return picture.opacity() === 255;
	   }
	   return false;
 };
	
	Game_Screen.prototype.fadeOutPicture = function(pictureId, duration) {
  	 	var picture = this.picture(pictureId);
  	 	if (picture) {
  			picture.fade(0, duration);
  		}
	};
	
	Game_Screen.prototype.fadeInPicture = function(pictureId, duration) {
  	 	var picture = this.picture(pictureId);
  	 	if (picture) {
  			picture.fade(255, duration);
  		}
	};
	
	Game_Screen.prototype.changePicture = function(pictureId, name) {
  	 	var picture = this.picture(pictureId);
  	 	if (picture) {
  			picture.change(name);
  		}
	};
		
	Game_Picture.prototype.change = function(name) {
 	   this._name = name;
	};
	
	
	Game_Picture.prototype.fade = function(opacity, duration) {
    	this._targetOpacity = opacity;
    	this._duration = duration;
	};
	
	Game_Interpreter.prototype.showSB = function(n) {
		var x = 408 + Math.randomInt(64) - 32;
		var y = 312 + Math.randomInt(64) - 32;
		var scaleX = 100 + Math.randomInt(50) - 25;
		var scaleY = 100 + Math.randomInt(50) - 25;
		var d = 10 + Math.randomInt(10) - 5;
		$gameScreen.showPicture(n, 'SB', 1, x, y, scaleX, scaleY, 0, 0);
		$gameScreen.movePicture(n, 1, x, y, 100, 100, 128, 0, d);
	};
	
	Game_Interpreter.prototype.eraseSB = function(n) {
		var x = Math.randomInt(128) - 64;
		var y = Math.randomInt(128) - 64;
		var d = 10 + Math.randomInt(10) - 5;
		$gameScreen.relativeMovePicture(n, 1, x, y, 100, 0, 0, 0, d);
	};
	
	Game_Screen.prototype.relativeMovePicture = function(pictureId, origin, x, y, scaleX, scaleY, opacity, blendMode, duration) {
		var picture = this.picture(pictureId);
		if (picture) {
			var x2 = picture.x + x;
			var y2 = picture.y + y;
			picture.move(origin, x2, y2, scaleX, scaleY, opacity, blendMode, duration);
		}
	};
})();
 
