package hmggirlfront.world;

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 hmggirlfront.mod_GirlFront;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;

public class WorldDifficultyLevel{
	
	/*public static int setDefaultLevel(mod_GirlFront mod, World world){
		//if(mod.proxy.getClient())return;
		int level = 0;
		
		level = mod.difficult_level;
	    return level;
	}
	
	public static int getLevel(mod_GirlFront mod, World world){
		int level = 0;
		level = mod.difficult_level;
	    return level;
	}
	
	
	
	public static void setLevel(mod_GirlFront mod, World world,  int lev, Entity en, String massage){
		int level = mod.difficult_level;
		int kazu = mod.difficult_level;
		System.out.println(String.format("updateLevel"));
		if(lev > level) {
			String name = world.getWorldInfo().getWorldName();
			File newfile = new File(".",
					"mods" + File.separatorChar + "hmggirlfront" + File.separatorChar+ "level"+ File.separatorChar + name);
			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
								{
									//System.out.println(String.format("2"));
									if (type[0].equals("level")) {
										level = Integer.parseInt(type[1]);
									}
								}
							}
						}
						br.close(); // ファイルを閉じる
					}
				} catch (FileNotFoundException ex) {
					ex.printStackTrace();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				if(level <= (lev - 1)) {
					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.close();
					} catch (FileNotFoundException ex) {
						ex.printStackTrace();
					} catch (IOException ex) {
						ex.printStackTrace();
					}
					mod.difficult_level = lev;
	    		}
			}else {
				//mod.proxy.ProxyFile(),
				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.close();
				} catch (FileNotFoundException ex) {
					ex.printStackTrace();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
				mod.difficult_level = lev;
    		}
			if(!en.world.isRemote){
				double k = en.posX;
				double l = en.posY;
				double i = en.posZ;
				int han = 10;
				AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - han), (double) (l - han),
						(double) (i - han), (double) (k + han), (double) (l + han), (double) (i + han)))
								.grow(1);
				List<Entity> llist = en.world.getEntitiesWithinAABBExcludingEntity(en, axisalignedbb);
				if (llist != null) {
					for (int lj = 0; lj < llist.size(); lj++) {
						Entity entity1 = (Entity) llist.get(lj);
						if (entity1.canBeCollidedWith() && entity1 != null) {
							if (entity1 instanceof EntityPlayer) {
								EntityPlayer player = (EntityPlayer) entity1;
								{
									player.sendMessage(new TextComponentTranslation(massage, new Object[0]));
								}
							}
						}
					}
				}
			}
			mod.difficult_level = lev;
		}else {
			
		}
	}
	
	 private static boolean checkBeforeReadfile(File file){
		    if (file.exists()){
		      if (file.isFile() && file.canRead()){
		        return true;
		      }
		    }

		    return false;
		  }*/
}