package hmggirlfront.event.level;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.network.GFClientMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class GFEventLevel_WorldIn {
	
	///ワールドにログイン時のイベント
	///レベルの設置
	
	@SubscribeEvent
	//public void OnEvent(WorldEvent.Load event) 
	public void OnEvent(EntityJoinWorldEvent event) 
	{
		mod_GirlFront mod = mod_GirlFront.INSTANCE;
		if(!event.getWorld().isRemote && event.getEntity() instanceof EntityPlayer){
			String name = event.getWorld().getWorldInfo().getWorldName();
			File newfile = new File(".",
					"mods" + File.separatorChar + "hmggirlfront" + File.separatorChar + "level" + File.separatorChar
							+ name + ".txt");
			if (newfile.isFile()) {
				try {
					if (checkBeforeReadfile(newfile)) {
						BufferedReader br = new BufferedReader(new FileReader(newfile)); // ファイルを開く
						String str;
						while ((str = br.readLine()) != null) { // 1行ずつ読み込む
							String[] type = str.split(",");
							if (type.length != 0) {// 1
								//if (!mod.proxy.getClient())
								{
									//System.out.println(String.format("1"));
									if (type[0].equals("level")) {
										mod.difficult_level = Integer.parseInt(type[1]);
										String dd = String.format("%1$3d", (int)mod.difficult_level);
										if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
											for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
								            {
												GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(1, mod.difficult_level), player);
								            }
							            }
										System.out.println("LEVEL"+String.format(dd));
									}
									if (type[0].equals("doll_kazu")) {
										mod.difficult_doll_kazu = Integer.parseInt(type[1]);
										String dd = String.format("%1$3d", (int)mod.difficult_doll_kazu);
										if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
											for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
								            {
												GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(2, mod.difficult_doll_kazu), player);
								            }
							            }
										System.out.println("KAZU"+String.format(dd));
									}
								}
							}
						}
						br.close(); // ファイルを閉じる
					}
				} catch (FileNotFoundException ex) {
					ex.printStackTrace();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			} else{
				File dire = new File(".",
						"mods" + File.separatorChar + "hmggirlfront" + File.separatorChar + "level");
				dire.mkdirs();
				try {
					newfile.createNewFile();
					BufferedWriter bw = new BufferedWriter(new FileWriter(newfile));
					bw.write("#");
					bw.newLine();
					bw.write("level," + "0");
					bw.newLine();
					bw.write("doll_kazu," + "0");
					bw.close();
				} catch (FileNotFoundException ex) {
					ex.printStackTrace();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				mod.difficult_level = 0;
				//mod.difficult_doll_kazu = 0;
				System.out.println(String.format("createLevel"));
				String dd = String.format("%1$3d", (int)mod.difficult_level);
				System.out.println(String.format(dd));
			}
		}
		
		
		//hmggirlfrontRecipeRegistrys_level.recipe(mod.difficult_level);
	}
	
	 private static boolean checkBeforeReadfile(File file){
		    if (file.exists()){
		      if (file.isFile() && file.canRead()){
		        return true;
		      }
		    }

		    return false;
		  }
	 
}