package hmggirlfront.entity.drone;

import java.util.List;

import gvclib.mod_GVCLib;
import gvclib.entity.EntityBBase;
import gvclib.entity.living.AI_EntityMoveS_Squad;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.EntityMAVBase;
import gvclib.entity.living.ISoldier;
import gvclib.event.GVCSoundEvent;
import gvclib.network.GVCLClientMessageKeyPressed;
import gvclib.network.GVCLMessageKeyPressed;
import gvclib.network.GVCLPacketHandler;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.items.ItemDollAttachment;
import hmggirlfront.items.ItemDollCN;
import hmggirlfront.items.ItemDollGunBase;
import hmggirlfront.network.GFClientMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIFleeSun;
import net.minecraft.entity.ai.EntityAIRestrictSun;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.item.EntityFireworkRocket;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import objmodel.IModelCustom;

public class EntityDrone extends EntityMAVBase {

	public int gun_class = 0;
	
	
	public ItemStack mainitem = new ItemStack((Item)null);// = nullはだめっぽい
	
	protected static final DataParameter<String> Model = 
    		EntityDataManager.<String>createKey(EntityDrone.class, DataSerializers.STRING);
    protected static final DataParameter<String> Tex = 
    		EntityDataManager.<String>createKey(EntityDrone.class, DataSerializers.STRING);
    
    protected static final DataParameter<String> Model_drone = 
    		EntityDataManager.<String>createKey(EntityDrone.class, DataSerializers.STRING);
    protected static final DataParameter<String> Tex_drone = 
    		EntityDataManager.<String>createKey(EntityDrone.class, DataSerializers.STRING);
    
    
	public IModelCustom model;
	public ResourceLocation tex;
	
	public IModelCustom model_drone;
	public ResourceLocation tex_drone;
	
	
	public EntityDrone(World worldIn) {
		super(worldIn);
		this.setSize(0.8F, 0.6F);
		this.biped = false;
		
		this.setSize(0.8F, 0.8F);
        this.setcanDespawn(1);
        this.setVehicleLock(true);
        
//        this.spawn_item = mod_GirlFront.comand_drone;
        
        this.ridding_nottarget = true;
        this.return_basepoint = true;
        
        this.ridding_invisible = true;
        this.ridding_sneakdismount = true;
        this.render_hud_information_1 = "Z-KEY:Get off";
        this.render_hud_information_2 = "X-KEY:Skill";
        this.render_hud_information_3 = "G-KEY:Order";
        this.render_hud_information_4 = "H-KEY:Squad";
        this.render_hud_information_5 = "L-Click:Doll Move";
        this.render_hud_information_6 = "R-Click:Zoom";
        
        this.riddng_freehand = false;
        this.hud_icon = "hmggirlfront:textures/hud/apc.png";
        vehicletype = 2;
        ridding_roteplayer = false;
        roodbreak = true;
        
        this.sp = 0.07F;
        this.turnspeed = 10F;
		this.riddng_opentop = true;
		this.renderhud = false;
		this.render_rader = false;
		this.stepHeight = 1.5F;
		this.riddng_maximum = 1;
		riddingx[0]=0.0D;
		riddingy[0]=-0.5D;
		riddingz[0]=0.0D;
		this.ridding_view1_x = 0.0F;
		this.ridding_view1_y = 0.3F;
		this.ridding_view1_z = 0.5F;
		
		
		ridding_view_x=0F;
		ridding_view_y=-1F;
		ridding_view_z=-2F;
		
		antibullet_0 = 1.0F;
		antibullet_1 = 1.0F;
		antibullet_2 = 1.0F;
		antibullet_3 = 1.0F;
		this.ridding_damege = 0.5F;
		
		{
			this.weapon1true = true;
    		this.weapon1key = 0;
    		this.w1name = "sizi";
    		this.ammo1 = 1;
    		this.magazine = 1;
    		this.reload_time1 = 20;
    		this.reloadsoundset1 = 40;
    		this.reloadsound1 = GVCSoundEvent.reload_shell;
		}
	}

	protected void initEntityAI() {
		this.tasks.addTask(1, new EntityAISwimming(this));
		this.tasks.addTask(2, new EntityAIRestrictSun(this));
		this.tasks.addTask(3, new EntityAIFleeSun(this, 1.0D));

	}

	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(200D);
		// this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(20D);
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.25D);
		this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(50.0D);
	}

	public void writeEntityToNBT(NBTTagCompound compound)
    {
        super.writeEntityToNBT(compound);
        NBTTagList nbttaglist = new NBTTagList();
        compound.setString("Model", this.getModel());
        compound.setString("Tex", this.getTex());
        compound.setString("Model_drone", this.getModel_drone());
        compound.setString("Tex_drone", this.getTex_drone());
    }
	public void readEntityFromNBT(NBTTagCompound compound)
    {
        super.readEntityFromNBT(compound);
        {
        	this.setModel(compound.getString("Model"));
        	this.setTex(compound.getString("Tex"));
        }
        {
        	this.setModel_drone(compound.getString("Model_drone"));
        	this.setTex_drone(compound.getString("Tex_drone"));
        }
    }
	protected void entityInit()
    {
    	super.entityInit();
    	this.dataManager.register(Model, new String("hmggirlfront:textures/model/drone/drone_command.mqo"));
        this.dataManager.register(Tex, new String("hmggirlfront:textures/model/drone/drone_command.png"));
        
        this.dataManager.register(Model_drone, new String("hmggirlfront:textures/model/drone/doll_drone.mqo"));
        this.dataManager.register(Tex_drone, new String("hmggirlfront:textures/model/drone/doll_drone.png"));
    }
	
	public String getModel() {
		return ((this.dataManager.get(Model)));
	}
	public void setModel(String s) {
		this.dataManager.set(Model, String.valueOf(new String(s)));
	}
	public String getTex() {
		return ((this.dataManager.get(Tex)));
	}
	public void setTex(String s) {
		this.dataManager.set(Tex, String.valueOf(new String(s)));
	}
	
	public String getModel_drone() {
		return ((this.dataManager.get(Model_drone)));
	}
	public void setModel_drone(String s) {
		this.dataManager.set(Model_drone, String.valueOf(new String(s)));
	}
	public String getTex_drone() {
		return ((this.dataManager.get(Tex_drone)));
	}
	public void setTex_drone(String s) {
		this.dataManager.set(Tex_drone, String.valueOf(new String(s)));
	}
	
	
	public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
    	ItemStack itemstack = player.getHeldItem(hand);
        boolean flag = !itemstack.isEmpty();
       if(player.isSneaking()) {
        	if (!this.world.isRemote)
            {
				if(!this.getHeldItemMainhand().isEmpty())this.entityDropItem(this.getHeldItemMainhand(), 0.0F);
                this.setDead();
            }
        	return true;
        }
        else {
        	return super.processInteract(player, hand);
        }
    }

	protected void onDeathUpdate() {
		if (!this.world.isRemote && this.deathTicks == 10)
        {
			if(!this.getHeldItemMainhand().isEmpty())this.entityDropItem(this.getHeldItemMainhand(), 0.0F);
        }
		super.onDeathUpdate();
	}
    
    
    public void onUpdate() {
		super.onUpdate();
		ItemStack main = this.getHeldItemMainhand();
		if(!main.isEmpty()){
			this.mainitem = main;
		}
		//何が何でも銃は手放さない
				if (this.getRidingEntity() == null) {// 1
					if( this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).isEmpty()) {
						if(!mainitem.isEmpty()) {
							this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, mainitem);
						}
					}
				}
				
		this.move_drone();
		BlockPos bp = this.world.getHeight(new BlockPos(this.posX, this.posY, this.posZ));
		double genY = bp.getY() + 1.5;
		if (!this.canBeSteered() && this.getControllingPassenger() == null && this.getHealth() > 0.0F)
		{
			if(this.deathTime < 0) {
				this.motionY *= 0.2F;
			}
			if (this.posY <= genY) {
				this.motionY = 0.1F;
			}
		}
    }
    
    public void move_drone() {
    	float f1 = this.rotationYawHead * (2 * (float) Math.PI / 360);
    	if (this.canBeSteered() && this.getControllingPassenger() != null && this.getHealth() > 0.0F)
		{
			if(this.getControllingPassenger() instanceof EntityPlayer)
			{
				EntityPlayer entitylivingbase = (EntityPlayer) this.getControllingPassenger();
				
				//entitylivingbase.height = 0.8F;
				{
					double x = this.posX;
					double y = this.posY;
					double z = this.posZ;
			  //      if (this.isAddedToWorld() && !this.world.isRemote) this.world.updateEntityWithOptionalForce(this, false); // Forge - Process chunk registration after moving.
			        float f = this.width / 2.0F;
			        float f12 = this.height;
			        entitylivingbase.setEntityBoundingBox(new AxisAlignedBB(x - (double)f, y, z - (double)f, x + (double)f, y + (double)f12, z + (double)f));
				}
				
				this.setcanDespawn(1);
				//entitylivingbase.rotationYawHead = this.rotationYaw;
				this.rotation = this.rotationYaw = this.renderYawOffset = entitylivingbase.rotationYawHead;
				this.rotationp = this.rotationPitch = entitylivingbase.rotationPitch;
				Vec3d looked = entitylivingbase.getLookVec();
				//PL_TankMove.movecar(entitylivingbase, this, sp, turnspeed);

				double xmove = 0;
				double zmove = 0;
				if (entitylivingbase.moveForward > 0.0F) {
					xmove -= MathHelper.sin(f1) * 0.05;
					zmove += MathHelper.cos(f1) * 0.05;
				}
				if (entitylivingbase.moveForward < 0.0F) {
					xmove -= MathHelper.sin(f1) * -0.05;
					zmove += MathHelper.cos(f1) * -0.05;
				}
				
				if (entitylivingbase.moveStrafing > 0.0F) {
					xmove -= MathHelper.sin(f1 - 1.57F) * 0.05;
					zmove += MathHelper.cos(f1 - 1.57F) * 0.05;
				}
				if (entitylivingbase.moveStrafing < 0.0F) {
					xmove -= MathHelper.sin(f1 - 1.57F) * -0.05;
					zmove += MathHelper.cos(f1 - 1.57F) * -0.05;
				}
				
				{
					this.motionX = xmove;
					this.motionZ = zmove;
					//entity.motionY = y;
					this.move(MoverType.PLAYER, this.motionX, this.motionY, this.motionZ);
				}
				boolean left = mod_GVCLib.proxy.leftclick();
				boolean right = mod_GVCLib.proxy.rightclick();
				boolean jump = mod_GVCLib.proxy.jumped();
				boolean kx = mod_GVCLib.proxy.keyx();
				boolean kg = mod_GVCLib.proxy.keyg();
				boolean kh = mod_GVCLib.proxy.keyh();
				boolean kc = mod_GVCLib.proxy.keyc();
				boolean kz = mod_GVCLib.proxy.keyz();
				if (left) {
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(11, this.getEntityId()));
					this.server1 = true;
				}
				if (right) {
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(12, this.getEntityId()));
				}
				if (jump) {
					this.serverspace = true;
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(13, this.getEntityId()));
					if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
						for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers()) {
							GVCLPacketHandler.INSTANCE2.sendTo(new GVCLClientMessageKeyPressed(13, this.getEntityId()), playermp);
						}
					}
				}
				if (kx) {
					this.serverx = true;
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(14, this.getEntityId()));
					if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
						for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers()) {
							GVCLPacketHandler.INSTANCE2.sendTo(new GVCLClientMessageKeyPressed(14, this.getEntityId()), playermp);
						}
					}
				}
				if (kg) {
					this.serverg = true;
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(15, this.getEntityId()));
					if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
						for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers()) {
							GVCLPacketHandler.INSTANCE2.sendTo(new GVCLClientMessageKeyPressed(15, this.getEntityId()), playermp);
						}
					}
				}
				if (kh) {
					this.serverh = true;
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(18, this.getEntityId()));
					if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
						for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers()) {
							GVCLPacketHandler.INSTANCE2.sendTo(new GVCLClientMessageKeyPressed(18, this.getEntityId()), playermp);
						}
					}
				}
				
				if (kz) {
					this.serverz = true;
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(17, this.getEntityId()));
					if(FMLCommonHandler.instance().getMinecraftServerInstance() != null) {
						for (EntityPlayerMP playermp : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayers()) {
							GVCLPacketHandler.INSTANCE2.sendTo(new GVCLClientMessageKeyPressed(17, this.getEntityId()), playermp);
						}
					}
				}
				double ymove = 0.0265;
				if (this.serverspace) {
					ymove = 0.10;
					this.serverspace = false;
				}
				/*if(this.serverz) {
					ymove = -0.1;
					this.serverz = false;
				}*/
				if(entitylivingbase.isSneaking()) {
					ymove = -0.1;
				}
				{
					this.motionY = ymove;
					this.move(MoverType.PLAYER, this.motionX, this.motionY, this.motionZ);
				}
				
				Drone_State.load(entitylivingbase, this);
				
				if (this.server1) {
					if(this.cooltime2 >= 10){
						if(entitylivingbase.world.isRemote)Drone_State.point_move(entitylivingbase, this);
						this.cooltime2 = 0;
					}
					this.server1 = false;
				}
				if (this.serverg) {
					if(entitylivingbase.world.isRemote)Drone_State.order(entitylivingbase, this);
					this.serverg = false;
				}
				if (this.serverh) {
					Drone_State.squad(entitylivingbase, this);
					this.serverh = false;
				}
				
				if (this.serverx) {
					Drone_State.skill(entitylivingbase, this);
					this.serverx = false;
				}
				
				
				
			}//player
			else if(this.getControllingPassenger() instanceof EntityGVCLivingBase) {
				
			}
		}
    }
}