//マイ画像クラス
class MyImage{
	constructor(){
		this.InitializeProgress = 0;
		this.imgChange = false;
		//画像オブジェクト設定
		for(var loop in imageSetting.imgFile){
			var tmpObj = [];			
			for(var index in imageSetting.imgFile[loop]){
				tmpObj[index] = imageSetting.imgDir[loop]+imageSetting.imgFile[loop][index];
				imageSetting.imgObj[loop] = tmpObj;
			}
		}
		this.backCorrect = 0;
		var backtime = 0;
		this.back_hiru = backtime++;
		this.back_yuu = backtime++;
		this.back_yoru = backtime++;
		this.back_sinya = backtime++;
		//画像
		this.img = imageSetting.imgObj;
		//画像個数
		this.ProgressCount = 0;
		this.imgCount = 0;
		//画像初期表示位置
		var x_init = 0;
		var y_init = 0;
		//画像オブジェクトからキー取得
		var imgKey = Object.keys(this.img);			//ループ用
		this.imgKeyArry = Object.keys(this.img);	//処理用画像名配列
		//各種処理用オブジェクト生成
		this.cavasTarget = {}						//描画ターゲット
		this.imgIndex = {}							//画像インデックス
		this.imgMagn = {}							//画像マージン
		this.imgPosXY = {}							//画像表示位置
		for(var loop in imgKey){
			this.imgCount += this.img[imgKey[loop]].length;//画像数カウント
			this.cavasTarget[imgKey[loop]]="image"+(parseInt(loop)==0?"":loop);//描画ターゲット設定
			this.imgIndex[imgKey[loop]] = 0;
			this.imgMagn[imgKey[loop]] = imageSetting.imgMagn[imgKey[loop]];
			this.imgPosXY[imgKey[loop]] = (parseInt(loop)==0?[0,0]:[x_init,y_init]);
		}
		//画像初期表示インデックス
		this.imgIndexInit = imageSetting.imgIndex;
		this.GraphStrokeRect = [150,10,100,20];
		this.GraphFillRect = [151,11,98,19];
	}
	
	//キャンバス生成
	CreateCanvas(){
		var imageViewSpase = _self.Elm("imageViewSpase",_self.TypeID);
		var imgKey = this.imgKeyArry;
		for(var loop in imgKey){
			var canvasId = this.cavasTarget[imgKey[loop]];
			var elmCnv = _self.CreateElm("canvas",canvasId);
			elmCnv.classList = "image_display";
			imageViewSpase.appendChild(elmCnv);
		}
	}
	
	//グラフ生成
	CreateGraph(graphId,MaxFlg=false){
		if((_self.Elm(graphId,_self.TypeID)==undefined)||(_self.Elm(graphId,_self.TypeID)==null)){
			var imageViewSpase = _self.Elm("imageViewSpase",_self.TypeID);
			var elmCnv = _self.CreateElm("canvas",graphId);
			elmCnv.classList = "image_display";
			imageViewSpase.appendChild(elmCnv);
			var canvas = _self.Elm(graphId,_self.TypeID);
			canvas.style.width = _self.width_hi+"px";
			var ctx = canvas.getContext('2d'); 
			ctx.strokeStyle  = 'deeppink';
			ctx.fillStyle = 'hotpink'; 
			var Rect = this.GraphStrokeRect;
			var Rect1 = this.GraphFillRect;
			ctx.strokeRect(Rect[0],Rect[1],Rect[2],Rect[3]);
			if(MaxFlg)ctx.fillRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]-1);
		}
		if(_self.Elm(graphId,_self.TypeID).style.display == "none"){
			_self.Elm(graphId,_self.TypeID).style.display = "";
		}
	}
	
	//グラフ描画
	ViewGraph(graphId,Value,MaxValue){
		var canvas = _self.Elm(graphId,_self.TypeID);
		var ctx = canvas.getContext('2d'); 
		ctx.strokeStyle  = 'deeppink'; 
		ctx.fillStyle   = 'hotpink'; 
		var Rect = this.GraphStrokeRect;
		var Rect1 = this.GraphFillRect;
		ctx.strokeRect(Rect[0],Rect[1],Rect[2],Rect[3]);
		if((Value/MaxValue)<=1)ctx.clearRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]);
		ctx.fillRect(Rect1[0],Rect1[1],Rect1[2]*(Value/MaxValue),Rect1[3]-1);	
	}
	//グラフ描画
	ViewGraphDec(graphId,Value,MaxValue){
		var canvas = _self.Elm(graphId,_self.TypeID);
		var ctx = canvas.getContext('2d'); 
		ctx.strokeStyle  = 'deeppink'; 
		ctx.fillStyle   = 'hotpink'; 
		var Rect = this.GraphStrokeRect;
		var Rect1 = this.GraphFillRect;
		ctx.strokeRect(Rect[0],Rect[1],Rect[2],Rect[3]);
		if((Value/MaxValue)<=1)ctx.fillRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]);
		ctx.clearRect(Rect1[0],Rect1[1],Rect1[2],Rect1[3]*(1-(Value/MaxValue)));
	}	
	
	//グラフ描画
	ClearGraph(graphId){
		_self.Elm(graphId,_self.TypeID).style.display = "none";
	}	
	
	
	//画像読込
	ImageLoad(index){
		var idx = parseInt(this.GetIndexPos(index));
		if(index=="back")idx = idx + parseInt(myImg.backCorrect);
		var url = this.GetImgUrl(index,idx);
		//画像設定
		var Data = _self.GetImage(index+idx);
		if(Data==null){
			Data = _self.GetDataStruct();
			Data.Name = index+idx;
			Data.Value = url;
			_self.ImageRead(Data);
			_self.AddImage(Data);
		}
	}
	
	//キャンバス描画済みチェック
	CanvasViewedStatus(index){
		var res = false;
		var imgCanvas = this.GetCanvasTarget(index);
		if(_self.ImageData[imgCanvas]!=undefined)res = true;
		return res;
	}
	
	//画像単体表示
	ImageView(index,wait=false,clear=false,target=false){
		//画像描画
		var idx = parseInt(this.GetIndexPos(index));
		if(index=="back")idx = idx + parseInt(myImg.backCorrect);		
		this.ImageLoad(index,idx);
		var Data = _self.GetImage(index+idx);
		var imgCanvas = this.GetCanvasTarget(index);			
		if(target)imgCanvas = target;
		_self.Elm(imgCanvas,_self.TypeID).style.position = "fixed";
		_self.Elm(imgCanvas,_self.TypeID).style.top = "0px";
		_self.Elm(imgCanvas,_self.TypeID).style.left = "0px";
		var x = this.GetImgPos(index,0);
		var y = this.GetImgPos(index,1);
		var Magn = this.GetImageMagn(index);
		if(!wait)_self.ViewCanvas(Data.Name,imgCanvas,x,y,Magn);
		if(wait)_self.ViewCanvasWait(Data.Name,imgCanvas,x,y,Magn);
	}
	
	//全消去
	AllClearImage(){
		for(var loop in this.imgKeyArry){
			this.ClearImage(this.imgKeyArry[loop]);
		}		
	}
	
	//描画消去
	ClearImage(index="back",rectData = [0,0,_self.width_hi, _self.height_hi]){
		var imgCanvas = this.GetCanvasTarget(index);		
		_self.ImageData[imgCanvas].clearRect(rectData[0],rectData[1],rectData[2],rectData[3]);
	}
	
	//読込進捗
	LoadProgeress(index){
		//画像描画
		var idx = parseInt(myImg.GetIndexPos(index));
		if(index=="back")idx = idx + parseInt(myImg.backCorrect);			
		var data = _self.GetImage(index+idx);
		if(data.flg){
			myImg.InitializeProgress = (++myImg.ProgressCount)/myImg.imgCount;
		}else{
			_self.setTimer(myImg.LoadProgeress,100,index);
		}
	}
	
	//初期表示
	InitImageView(){
		this.InitializeProgress = 0;
		var timerCount = 0;
		var timevalue = 0;
		myImg.imgCount += this.imgKeyArry.length;
		for(var loop in this.imgKeyArry){
			timevalue = parseInt(loop*50);
			var count = this.GetImageCount(this.imgKeyArry[loop]);
			for(var loop2=0;loop2<count;loop2++){
				this.SetIndexPos(this.imgKeyArry[loop],loop2);
				_self.setTimer(function(param){
					myImg.ImageView(myImg.imgKeyArry[param],true);
					myImg.ImageLoad(myImg.imgKeyArry[param]);
					myImg.LoadProgeress(myImg.imgKeyArry[param]);
				},timevalue,loop);
				timevalue += 10;
			}
			timerCount += timevalue;
		}
		_self.setTimer(function(){
			timerCount = 0;
			for(var loop in myImg.imgKeyArry){
				timevalue = parseInt(loop*50)+100;
				var index = myImg.GetIndexInitPos(myImg.imgKeyArry[loop]);
				myImg.SetIndexPos(myImg.imgKeyArry[loop],index);
				_self.setTimer(function(param){
					myImg.ImageView(myImg.imgKeyArry[param],true,true);
					myImg.imgChange = false;
					myImg.LoadProgeress(myImg.imgKeyArry[param]);
				},timevalue,loop);
				timerCount += timevalue;
			}		
		},timerCount);
		_self.setTimer(function(){
			myImg.ProgressCheck = function(){
				if(parseInt(myImg.InitializeProgress*100)==100){
					Main.imageload = true;
					this.InitializeProgress = 100;
				}else{
					_self.setTimer(myImg.ProgressCheck,500);
				}
			}
			myImg.ProgressCheck();
		},100);		
	}
	
	//全画像
	AllImageView(wait=false,clear=true,target=false){
		if(target)_self.Elm(target,_self.TypeID).style.display = "";
		for(var loop in this.imgKeyArry){
			this.ImageView(this.imgKeyArry[loop],wait,clear,target);
			this.imgChange = false;
		}
		if(target)_self.Elm(target,_self.TypeID).style.display = "";
	}	
	
	//画像オブジェクトデータ取得
	GetImgObjParam(key,obj){
		var res = null;
		var keyArray = this.imgKeyArry;
		for(var loop in this.imgKeyArry){
			if(keyArray[loop]==key){
				res = obj[key];
			}
		}
		return res;
	}
	
	//画像数取得
	GetImageCount(key){
		var res = null;
		res = myImg.GetImgObjParam(key,this.img);
		return res.length;		
	}
	
	//画像表示先取得
	GetCanvasTarget(key){
		var res = null;
		res = myImg.GetImgObjParam(key,this.cavasTarget);
		return res;		
	}

	//画像倍率取得
	SetImageMagn(key,value){
		if(this.imgKeyArry.indexOf(key)!==-1){
			this.imgMagn[key] = value;
			this.imgChange = true;
		}		var res = null;
	}

	//画像倍率取得
	GetImageMagn(key){
		var res = null;
		res = myImg.GetImgObjParam(key,this.imgMagn);
		return res;		
	}
	
	//画像インデックス設定
	SetIndexPos(key,value){
		if(typeof value==='object')value = value.back;
		if(this.imgKeyArry.indexOf(key)!==-1){
			this.imgIndex[key] = value;
			this.imgChange = true;
		}
	}	
	
	//画像インデックス取得
	GetIndexPos(key){
		var res = null;
		res = myImg.GetImgObjParam(key,this.imgIndex);
		return res;		
	}
	
	//画像初期インデックス取得
	GetIndexInitPos(key){
		var res = null;
		res = myImg.GetImgObjParam(key,this.imgIndexInit);
		return res;		
	}			
	
	//画像座標取得
	SetImgPos(key,xy_value){
		if(this.imgPosXY[key]){
			this.imgPosXY[key] = xy_value;
			this.imgChange = true;
		}
	}
	
	//画像座標取得
	GetImgPos(key,xy_value){
		var res = null;
		res = myImg.GetImgObjParam(key,this.imgPosXY);
		return res[xy_value];
	}
	
	//画像取得
	GetImgUrl(key,index){
		var res = null;
		res = myImg.GetImgObjParam(key,this.img);
		return res[index];
	}
}
myImg = new MyImage();