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 java.util.List;

import gvclib.event.GVCSoundEvent;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.network.GFClientMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class WorldDifficultyLevel_new{
	
	@SideOnly(Side.CLIENT)
	public static int getLevelClient(){
		if(mod_GirlFront.cfg_worldlevel_debag) {
			mod_GirlFront.INSTANCE.difficult_level = 1000;
			return 1000;
		}else {
			 return mod_GirlFront.INSTANCE.difficult_level;
		}
	}
	
	public static int getLevel(){
		if(mod_GirlFront.cfg_worldlevel_debag) {
			mod_GirlFront.INSTANCE.difficult_level = 1000;
			return 1000;
		}else {
			 return mod_GirlFront.INSTANCE.difficult_level;
		}
	}
	
	public static int getKazu(){
		if(mod_GirlFront.cfg_worldlevel_debag) {
			mod_GirlFront.INSTANCE.difficult_doll_kazu = 1000;
			return 1000;
		}else {
			 return mod_GirlFront.INSTANCE.difficult_doll_kazu;
		}
	}
	
	public static void setLevel(mod_GirlFront mod, World world,  int lev, String massage){
		int level = mod.difficult_level;
		System.out.println(String.format("updateLevel"));
		if(lev > level  && !world.isRemote) {
			String name = world.getWorldInfo().getWorldName();
			File newfile = new File(".",
					"mods" + File.separatorChar + "hmggirlfront" + File.separatorChar+ "level"+ File.separatorChar + name + ".txt");
			if(newfile.isFile()) {
				{
					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," + String.valueOf(lev));
						bw.newLine();
						bw.write("doll_kazu," + String.valueOf(mod.difficult_doll_kazu));
						bw.close();
					} catch (FileNotFoundException ex) {
						ex.printStackTrace();
					} catch (IOException ex) {
						ex.printStackTrace();
					}
					mod.difficult_level = lev;
	    		}
			}else {//ファイルを作成
				
    		}
			{
				EntityPlayer player = mod.proxy.getEntityPlayerInstance();
				if(player != null) {
			//		if(player.world.isRemote)player.sendMessage(new TextComponentTranslation(massage, new Object[0]));
					player.playSound(SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, 0.5F, 1.0F);
				}
			}
			mod.difficult_level = lev;
			if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
				for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers())
	            {
					GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(1, mod.difficult_level), player);
					GFPacketHandler.INSTANCE2.sendTo(new GFClientMessageKeyPressed(2, mod.difficult_doll_kazu), player);
					if(player != null) {
						player.sendMessage(new TextComponentTranslation(massage, new Object[0]));
						//player.playSound(SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, 0.5F, 1.0F);
					}
	            }
            }
		}else {
			
		}
	}
	
	public static void setKazu(mod_GirlFront mod, World world,  int lev, String massage){
		//int level = mod.difficult_doll_kazu;
		System.out.println(String.format("updateKazu"));
		{
			String name = world.getWorldInfo().getWorldName();
			File newfile = new File(".",
					"mods" + File.separatorChar + "hmggirlfront" + File.separatorChar+ "level"+ File.separatorChar + name + ".txt");
			if(newfile.isFile()) {
				{
					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," + String.valueOf(mod.difficult_level));
						bw.newLine();
						bw.write("doll_kazu," + String.valueOf(lev));
						bw.close();
					} catch (FileNotFoundException ex) {
						ex.printStackTrace();
					} catch (IOException ex) {
						ex.printStackTrace();
					}
					mod.difficult_doll_kazu = lev;
	    		}
			}else {//ファイルを作成
				
    		}
			
			mod.difficult_doll_kazu = lev;
		}
	}
	
	
	 private static boolean checkBeforeReadfile(File file){
		    if (file.exists()){
		      if (file.isFile() && file.canRead()){
		        return true;
		      }
		    }

		    return false;
		  }
}