package hmggirlfront.entity.vehicle;

import java.util.List;

import gvclib.mod_GVCLib;
import gvclib.entity.living.AI_EntityWeapon;
import gvclib.entity.living.AI_TankSet;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.EntityMobVehicleBase;
import gvclib.entity.living.ISoldier;
import gvclib.entity.living.PL_TankMove;
import gvclib.event.GVCSoundEvent;
import gvclib.network.GVCLMessageKeyPressed;
import gvclib.network.GVCLPacketHandler;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class EntityVe_APC extends EntityMobVehicleBase
{

    public EntityVe_APC(World worldIn)
    {
        super(worldIn);
        this.setSize(4F, 3.0F);
        this.setcanDespawn(1);
        this.setVehicleLock(true);
        this.riddng_freehand = false;
        this.hud_icon = "hmggirlfront:textures/hud/apc.png";
        //this.render_hud_icon_hori = true;
        //this.hud_icon_hori = "hmggirlfront:textures/hud/heli_hori.png";
        this.render_hud_information_2 = "R-Click:Zoom";
        this.render_hud_information_3 = "W/S-KEY:Throttle-UP/DWON";
        this.render_hud_information_4 = "A/D-KEY:TURN-RIGHT/LEFT";
        this.render_hud_information_5 = "V-KEY:ALLY-Boarding";
        this.render_hud_information_6 = "B-KEY:ALLY-Get off";
        vehicletype = 2;
        ridding_roteplayer = false;
        roodbreak = mod_GirlFront.cfg_vehicle_breakblock;
        this.spawn_item = mod_GirlFront.ve_apc;
        
        
        vehicle_ridding_nofire[0] = true;
        vehicle_ridding_nofire[1] = true;
        vehicle_ridding_nofire[2] = true;
        vehicle_ridding_nofire[3] = true;
        vehicle_ridding_nofire[4] = true;
        vehicle_ridding_nofire[5] = true;
        
        this.sp = 0.045F;
        this.turnspeed = 2F;
		this.ridding_invisible = false;
		this.riddng_opentop = false;
		this.renderhud = true;
		this.thmax = 5F;
		this.thmin = -2F;
		this.thmaxa = 0.2F;
		this.thmina = -0.15F;
		this.stepHeight = 1.5F;
		this.riddng_maximum = 6;
		riddingx[0]=0.3D;
		riddingy[0]=0.9D;
		riddingz[0]=1.5D;
		{
			riddingx[1]=0.0D;
			riddingy[1]=2.0D;
			riddingz[1]=-0.6D;
			
			riddingx[2]=0.8D;
			riddingy[2]=0.9D;
			riddingz[2]=0.2D;
			
			riddingx[3]=-0.8D;
			riddingy[3]=0.9D;
			riddingz[3]=0.2D;
			
			riddingx[4]=0.8D;
			riddingy[4]=0.9D;
			riddingz[4]=-1.0D;
			riddingx[5]=-0.8D;
			riddingy[5]=0.9D;
			riddingz[5]=-1.0D;
			
		}
		
		
		ridding_view_x=0F;
		ridding_view_y=-2F;
		ridding_view_z=-5F;
		ridding_view_gunner_x=0F;
		ridding_view_gunner_y=2F;
		ridding_view_gunner_z=1F;
		
		antibullet_0 = 0.0F;
		antibullet_1 = 0.5F;
		antibullet_2 = 1.0F;
		antibullet_3 = 1.1F;
		this.ridding_damege = 0.5F;
		
		
		mob_min_range = 10;
		mob_max_range = 30;
		
		
    }

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(200D);
        this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(20D);
    }

    public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
    	ItemStack itemstack = player.getHeldItem(hand);
        boolean flag = !itemstack.isEmpty();
        if(!itemstack.isEmpty()&& itemstack.getItem() == mod_GirlFront.idoll_parts)
        {
        	if(this.getHealth() < this.getMaxHealth()) {
				this.setHealth(this.getHealth() + 10);
    			itemstack.shrink(1);
    			this.playSound(SoundEvents.BLOCK_ANVIL_LAND, 5F, 2F);
			}
        	return true;
        }
        else  if(!itemstack.isEmpty()&& itemstack.getItem() == mod_GirlFront.ve_weaponchange)
        {
        	this.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1.0F);
        	if(this.getArmID_L() == 0) {
        		this.setArmID_L(1);
        		this.setRemain_L(1);
        	}else {
        		this.setArmID_L(0);
        		this.setRemain_L(30);
        	}
        	return true;
        }
       /* else  if(!itemstack.isEmpty()&& itemstack.getItem() == mod_GirlFront.dollgun_galil)
        {
        	itemstack.shrink(1);
        	this.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1.0F);
        	this.setArmID_L(1);
        	this.setRemain_L(1);
        	return true;
        }
        else if(!itemstack.isEmpty()&& itemstack.getItem() == mod_GirlFront.dollgun_m1911)
        {
        	itemstack.shrink(1);
        	this.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1.0F);
        	this.setArmID_L(0);
        	this.setRemain_L(30);
        	return true;
        }*/
        else{
        	super.processInteract(player, hand);
        	return true;
        }
        /*
        if(!itemstack.isEmpty()&& itemstack.getItem() == mod_GirlFront.idoll_parts)
        {
        	this.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1.0F);
        	this.setArmID_L(1);
        	this.setRemain_L(1);
        	return true;
        }
        else if(!itemstack.isEmpty()&& itemstack.getItem() == mod_GirlFront.idoll_bullet)
        {
        	this.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1.0F);
        	this.setArmID_L(0);
        	this.setRemain_L(30);
        	return true;
        }
        else if(!itemstack.isEmpty()&& itemstack.getItem() == mod_GirlFront.idoll_hp)
        {
        	this.playSound(SoundEvents.BLOCK_ANVIL_HIT, 1.0F, 1.0F);
        	this.setArmID_L(2);
        	this.setRemain_L(30);
        	return true;
        }
        else{
        	super.processInteract(player, hand);
        	return true;
        }*/
        
    }
    
    public boolean spawn = false;
    public void onUpdate() {
		super.onUpdate();
		this.setcanDespawn(1);
		this.riddng_freehand = false;
		float f1 = this.rotationYawHead * (2 * (float) Math.PI / 360);
		this.roadattack();
		this.setcanDespawn(1);
		//this.typeHeli(GVCSoundEvent.sound_heli);
		this.setVehicleLock(true);
		if(this.getArmID_L() == 2) {
			if (this.world
					.getBlockState(new BlockPos(this.posX, this.posY + 0.8, this.posZ))
					.getMaterial() == Material.WATER) {
				this.motionY = 0.05F;
			}
			if(this.collidedHorizontally && this.isWet()){
				//this.stepHeight = 2F;
				this.motionY = 0.2F;
			}
		}
		
		if(this.getArmID_L() == 1){
			
			this.ridding_rotation_lock = true;
	        this.rotation_max = 45F;
	        this.rotationp_max = -20F;
	        this.rotationp_min = 10F;
			
			riddingx[1]=0.0D;
			riddingy[1]=0.9D;
			riddingz[1]=-0.6D;
			
			this.weapon1true = true;
    		this.weapon1key = 0;
    		this.w1name = "105mmCannon";
    		this.ammo1 = 10;
    		this.magazine = 1;
    		this.reload_time1 = 80;
    		this.reloadsoundset1 = 20;
    		this.reloadsound1 = GVCSoundEvent.reload_cannon;
    		this.w1cycle = 1;
    		this.w1barst = 1;
    		this.weapon1 = 1;
    		{
    			this.bullet_type1[0] = 3;
        		this.bullet_model1[0] = "gvclib:textures/entity/bulletcannon.mqo";
        		this.bullet_tex1[0] = "gvclib:textures/entity/bulletcannon.png";
        		this.fire_model1[0]= "gvclib:textures/entity/msmoke.mqo";
        		this.fire_tex1[0] = "gvclib:textures/entity/msmoke.png";
        		this.fire_time1[0] = 16;
        		this.sound_fire1[0] = "gvclib.fire_cannon2";
        		this.fire_pointx1[0] = 0;
        		this.fire_pointy1[0] = 3.25F;
        		this.fire_pointz1[0] = 4.0F;
        		this.basis_pointx1[0] = 0;
        		this.basis_pointy1[0] = 3.25F;
        		this.basis_pointz1[0] = 0F;
        		this.fire_yoffset1[0] = 0;
        		this.rotationfollowing1[0] = true;
        		this.rotation_player1[0] = true;
        		this.rotationfirepoint1[0] = false;
        		this.bullet_damage1[0] = 60;
        		this.bullet_speed1[0] = 5.0F;
        		this.bullet_bure1[0] = 0.5F;
        		this.bullet_expower1[0] = 2.0F;
        		this.bullet_ex1[0] = false;
        		this.bullet_kazu1[0] = 1;
        		this.bullet_gravity1[0] = 0.025D;
        		this.bullet_livingtime1[0] = 80;
    		}
			this.mob_min_range = 15;
			this.mob_max_range = 50;
			this.mob_w1range = 50;
			this.mob_w1range_max_y = 20;
			this.mob_w1range_min_y = 5;
		}else {
			
			this.ridding_rotation_lock = false;
	        this.rotation_max = 180F;
	        this.rotationp_max = -90F;
	        this.rotationp_min = 90F;
			
			riddingx[1]=0.0D;
			riddingy[1]=2.2D;
			riddingz[1]=-0.6D;
			
			this.weapon1true = true;
			this.weapon1key = 0;
			this.ammo1 = 3;
			this.magazine = 30;
			reload_time1 = 100;
			this.w1name = "MachineGun";
			this.w1cycle = 1;
			this.w1barst = 1;
			this.weapon1 = 1;
			{
				this.bullet_type1[0] = 0;
				this.bullet_model1[0] = "gvclib:textures/entity/BulletNormal.obj";
				this.bullet_tex1[0] = "gvclib:textures/entity/BulletNormal.png";
				this.fire_model1[0] = "gvclib:textures/entity/mflash.mqo";
				this.fire_tex1[0] = "gvclib:textures/entity/mflash.png";
				this.fire_time1[0] = 2;
				this.sound_fire1[0] = "gvclib.fire_rifle";
				this.fire_pointx1[0] = 0;
				this.fire_pointy1[0] = 3.1F;
				this.fire_pointz1[0] = 2.3F;
				this.basis_pointx1[0] = 0;
        		this.basis_pointy1[0] = 3.1F;
        		this.basis_pointz1[0] = 0.35F;
				this.fire_yoffset1[0] = 0;
				this.rotationfollowing1[0] = true;
        		this.rotation_player1[0] = true;
        		this.rotationfirepoint1[0] = false;
        		this.rotation_mob_head1[0] = true;
        		this.rotation_firepointbxbz1[0] = true;
        		this.arm_x[0] = 0.0F;
        		this.arm_z[0] = 0.0F;
				this.bullet_damage1[0] = 8;
				this.bullet_speed1[0] = 3.5F;
				this.bullet_bure1[0] = 1.0F;
				this.bullet_expower1[0] = 0F;
				this.bullet_ex1[0] = false;
				this.bullet_kazu1[0] = 1;
				this.bullet_gravity1[0] = 0.029D;
				this.bullet_livingtime1[0] = 80;

			}
			this.mob_min_range = 15;
			this.mob_max_range = 30;
			this.mob_w1range = 30;
			this.mob_w1range_max_y = 20;
			this.mob_w1range_min_y = 5;
		}
		
		this.ContMobRidding();
		
		
		if (this.canBeSteered() && this.getControllingPassenger() != null && this.getHealth() > 0.0F)
		{
			if(this.getControllingPassenger() instanceof EntityPlayer)
			{
				EntityPlayer entitylivingbase = (EntityPlayer) this.getControllingPassenger();
				this.setcanDespawn(1);
				if(this.getArmID_L() == 1) {
					this.rotation  = entitylivingbase.rotationYawHead;
					this.rotationp = this.rotationPitch = entitylivingbase.rotationPitch;
				}
				//this.rotation  = entitylivingbase.rotationYawHead;
				//this.rotationp = this.rotationPitch = entitylivingbase.rotationPitch;
				Vec3d looked = entitylivingbase.getLookVec();
				PL_TankMove.move2(entitylivingbase, this, sp, turnspeed);

				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 kc = mod_GVCLib.proxy.keyc();
				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) {
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(13, this.getEntityId()));
				}
				if (kx) {
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(14, this.getEntityId()));
				}
				if (kg) {
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(15, this.getEntityId()));
				}
				if (kc) {
					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(16, this.getEntityId()));
				}


				if (serverx) {
					this.serverx = false;
				}

				if (this.server1 && this.getArmID_L() == 1) {
					{
	    				if(cooltime >= ammo1){
	    					this.counter1 = true;
	    		            cooltime = 0;
	    				}
	    				if(this.weapon1true) {
	    					{
	    						this.weapon1active(looked, entitylivingbase);
	    					}
	        		    }
	    			}
					this.server1 = false;
				}

				
			}//player
			else if(this.getControllingPassenger() instanceof EntityGVCLivingBase) {
				
			}
		}
		//Entity entity = this.getRidingEntity();
		//if (this.isPassenger(entity))
		if(this.getPassengers().size() > 1 && this.getArmID_L() != 1)
        {
        	// int i = this.getPassengers().indexOf(entity);
        	 //if (i == 1) 
        	 if(this.getPassengers().get(1) != null){
     			if (this.getPassengers().get(1) instanceof EntityPlayer) {
     				EntityPlayer entitylivingbase = (EntityPlayer)this.getPassengers().get(1);
     				this.setcanDespawn(1);
     				this.rotation_1  = entitylivingbase.rotationYawHead;
     				this.rotation  = entitylivingbase.rotationYawHead;
     				this.rotationp_1 = entitylivingbase.rotationPitch;
     				Vec3d looked = entitylivingbase.getLookVec();
     				boolean left = mod_GVCLib.proxy.leftclick();
     				if (left) {
     					GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(11, this.getEntityId()));
     					this.server1 = true;
     				}

     				if (this.server1) {
     					{
     	    				if(cooltime >= ammo1){
     	    					this.counter1 = true;
     	    		            cooltime = 0;
     	    				}
     	    				if(this.weapon1true) {
     	    					if(this.getArmID_R() == 1 && this.weapon11true){
     	    						this.weapon11active(looked, entitylivingbase);
     	    					}else if(this.getArmID_R() == 2 && this.weapon12true){
     	    						this.weapon11active(looked, entitylivingbase);
     	    					}else {
     	    						this.weapon1active(looked, entitylivingbase);
     	    					}
     	        		    }
     	    			}
     					this.server1 = false;
     				}
     			}else
     			if (this.getPassengers().get(1) instanceof EntityDoll) {
     				EntityDoll entitylivingbase = (EntityDoll)this.getPassengers().get(1);
     				if(entitylivingbase.getattacktask()) {
     					
     				}else {
     					entitylivingbase.rotationYawHead = this.rotationYaw;
     				}
     				this.rotation_1  = entitylivingbase.rotationYawHead;
     				this.rotation  = entitylivingbase.rotationYawHead;
     				this.rotationp_1 = entitylivingbase.rotationPitch;
     				Vec3d looked = entitylivingbase.getLookVec();
     				AI_EntityWeapon.getTargetEntity(entitylivingbase, this.mob_w1range, this.mob_w1range_max_y, this.mob_w1range_min_y);
     				if(AI_EntityWeapon.getRange(entitylivingbase, this.mob_w1range, this.mob_w1range_max_y, this.mob_w1range_min_y)){
     					if(cooltime >= ammo1 && this.getRemain_L() > 0){
     						this.counter1 = true;
     			            cooltime = 0;
     					}
     					if(this.weapon1true) {
     	    		    	this.weapon1activeMob(looked, entitylivingbase, 0);
     	    		    }
     				}
     			}
     		}
        }
		AI_TankSet.set2(this, GVCSoundEvent.sound_tank, f1, sp, 0.1F);
		
    }

   
}