package hmggirlfront.entity;

import java.util.List;
import java.util.Random;

import javax.annotation.Nullable;

import gvclib.mod_GVCLib;
import gvclib.entity.living.EntityMobVehicleBase;
import gvclib.entity.living.EntityVehicleBase;
import gvclib.entity.living.ISoldier;
import gvclib.entity.living.cnt.VehicleCNT_Tank_SPG;
import gvclib.entity.living.cnt.VehicleCNT_Turret;
import gvclib.item.ItemWrench;
import gvclib.network.GVCLMessageKeyPressed;
import gvclib.network.GVCLPacketHandler;
import gvclib.world.GVCExplosionBase;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.items.ItemDollGun_HL;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IEntityLivingData;
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.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;

public class EntityDoll_HL extends EntityMobVehicleBase
{
    public EntityDoll_HL(World worldIn)
    {
        super(worldIn);
        this.setSize(2.5F, 2.3F);
        ridding_roteangle = true;
    }

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        
        this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(20D);
    }
    
    
    
    public boolean vacanspawn = false;
    public int spawn_rare = 10;
    
    public ItemStack mainitem = new ItemStack((Item)null);// = nullはだめっぽい
    
    
    
    /**HMV2専用?
     *あとでVehicleBaseに移植するはず*/
    
    public boolean bomber_sighting = false;
    public boolean spg_sighting = false;
   
    
    public int exhaust = 0;
    public float[] exhaust_x = new float[32];
    public float[] exhaust_y = new float[32];
    public float[] exhaust_z = new float[32];
    public boolean exhaust_ab = false;
    
    public int laser_sight = 0;
    public float[] laser_sight_x = new float[32];
    public float[] laser_sight_y = new float[32];
    public float[] laser_sight_z = new float[32];
    public int[] laser_sight_weapon = new int[32];
    public boolean[] rote_laser_sight = new boolean[32];
    
    
    //内部用
    public int weapon_mode = 0;
    boolean reset = false;
    
    
    public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
    	ItemStack itemstack = player.getHeldItem(hand);
        boolean flag = !itemstack.isEmpty();
    	if(this.getHealth() >= this.getMaxHealth() && this.getHealth() > 0.0F && player.isSneaking()) 
		{
    		if (!this.world.isRemote)
            {
    			this.entityDropItem(new ItemStack(spawn_item), 0.0F);
                this.setDead();
            }
    		return true;
		}else {
			if(flag){
        		if(itemstack.getItem() == mod_GirlFront.idoll_parts) {
        			this.setHealth(this.getHealth() + 10);
        			this.playSound(SoundEvents.BLOCK_ANVIL_LAND, 1F, 2F);
        		}
			}
			return super.processInteract(player, hand);
		}
    }
    
    
    public void readinformation() {
    	ItemStack itemstack = this.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
    	if(itemstack.getItem() instanceof ItemDollGun_HL && !itemstack.isEmpty()) {//item_start
    		
    		mainitem = itemstack;
    		spawn_item = itemstack.getItem();
    		
    		this.modid = mod_GirlFront.MOD_ID;
    		ItemDollGun_HL head = (ItemDollGun_HL) itemstack.getItem();
    		//this.setModel(head.model);
    		//this.setTex(head.tex);
			if (mod_GirlFront.proxy.getClient()) {
				this.model = head.dollhl_model;
				this.tex = new ResourceLocation(head.dollhl_tex);
			}
    		
    		//System.out.println(String.format("handmadevehicle-"));
    		
    		
    		
    		
    		this.maxhp = 200;
    		this.stepHeight = head.stepHeight;
    		//this.setwidth = head.setwidth;
    		
    		if(!reset) {
				this.ridding_roteplayer = head.ridding_roteplayer;
				this.riddng_maximum = head.riddng_maximum;
				for (int w1 = 0; w1 < riddng_maximum; ++w1) {
					this.riddingx[w1] = head.riddingx[w1];
					this.riddingy[w1] = head.riddingy[w1];
					this.riddingz[w1] = head.riddingz[w1];
					this.riddingx_roteplayer[w1] = head.riddingx_roteplayer[w1];
					this.riddingy_roteplayer[w1] = head.riddingy_roteplayer[w1];
					this.riddingz_roteplayer[w1] = head.riddingz_roteplayer[w1];
				}

				this.ridding_view1_x = head.ridding_view1_x;
				this.ridding_view1_y = head.ridding_view1_y;
				this.ridding_view1_z = head.ridding_view1_z;

				this.ridding_view_x = head.ridding_view_x;
				this.ridding_view_y = head.ridding_view_y;
				this.ridding_view_z = head.ridding_view_z;

				this.ridding_view_gunner_x = head.ridding_view_gunner_x;
				this.ridding_view_gunner_y = head.ridding_view_gunner_y;
				this.ridding_view_gunner_z = head.ridding_view_gunner_z;

				this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(maxhp);
				this.setHealth((float) head.maxhp);
				this.setSize(head.setwidth, head.setheight);
    		}
    		
    		this.ridding_rote = head.ridding_rote;
    		
    		this.ridding_invisible = head.ridding_invisible;
    		this.ridding_zoom = head.ridding_zoom;
    		this.hud_icon = head.hud_icon;
    		this.render_hud_icon_hori = head.render_hud_icon_hori;
    		this.hud_icon_hori = head.hud_icon_hori;
    		this.renderhud = head.renderhud;
    		
    		this.hud_icon_scope = head.hud_icon_scope;
    		this.render_hud_scope = head.render_hud_scope;
    		this.hud_icon_scope_zoom = head.hud_icon_scope_zoom;
    		this.render_hud_scope_zoom = head.render_hud_scope_zoom;
    		this.render_rader = head.render_rader;
    		
    		this.aarader = head.aarader;
    		
    		this.vehicletype = head.vehicletype;
    		if(head.movesound != null)this.movesound = SoundEvent.REGISTRY.getObject(new ResourceLocation(modid, head.movesound));
    		
    		
    		this.riddng_opentop = head.riddng_opentop;
    		this.antibullet_0 = head.antibullet_0;
    		this.antibullet_1 = head.antibullet_1;
    		this.antibullet_2 = head.antibullet_2;
    		this.antibullet_3 = head.antibullet_3;
    		this.ridding_damege = head.ridding_damege;
    		
    		
    		
    	    this.damage_front = head.damage_front;
    	    this.damage_side = head.damage_side;
    	    this.damage_rear = head.damage_rear;
    	    this.damage_top = head.damage_top;
    	    this.damage_bottom = head.damage_bottom;
    	    this.can_turret = head.can_turret;
    	    this.turret_height = head.turret_height;
    	    this.damage_turret_front = head.damage_turret_front;
    	    this.damage_turret_side = head.damage_turret_side;
    	    this.damage_turret_rear = head.damage_turret_rear;
    		
    	    this.roodbreak = head.roodbreak;
    	    {
    	    	this.roodbreak = false;
    	    }
    		
    		this.sp = head.sp;
    		this.turnspeed = head.turnspeed;
    		
    		this.spg_sighting = head.spg_sighting;
    		this.spg_min_range = head.spg_min_range;
    		
    		this.thmax = head.thmax;
    		this.thmin = head.thmin;
    		this.thmaxa = head.thmaxa;
    		this.thmina = head.thmina;
    		
    		this.rotationp_max =   head.rotationp_max;
    		this.rotationp_min =   head.rotationp_min;
    		
    		this.ridding_rotation_lock =   head.ridding_rotation_lock;
    		this.rotation_max =   head.rotation_max;
    		
    		this.model_angle_base_x = head.model_angle_base_x;
    		this.model_angle_base_y = head.model_angle_base_y;
    		this.model_angle_base_z = head.model_angle_base_z;
    		this.model_angle_offset_x = head.model_angle_offset_x;
    		this.model_angle_offset_y = head.model_angle_offset_y;
    		this.model_angle_offset_z = head.model_angle_offset_z;
    		this.model_angle_x = head.model_angle_x;
    		this.model_angle_y = head.model_angle_y;
    		this.model_angle_z = head.model_angle_z;
    		
    		this.mob_min_range = head.mob_min_range;
    		this.mob_max_range = head.mob_max_range;
    		this.mob_min_height = head.mob_min_height;
    		
    		this.turret = head.turret;
    		for(int t1 = 0; t1 < turret; ++t1) {
    			this.turretrote[t1] = head.turretrote[t1];
    			this.arm_x[t1] = head.arm_x[t1];
        		this.arm_y[t1] = head.arm_y[t1];
        		this.arm_z[t1] = head.arm_z[t1];
        	    
        		this.hand_x[t1] = head.hand_x[t1];
        		this.hand_y[t1] = head.hand_y[t1];
        		this.hand_z[t1] = head.hand_z[t1];
        		
        		this.finger_type[t1] = head.finger_type[t1];
    		}
    		
    		this.pera = head.pera;
    		for(int t1 = 0; t1 < pera; ++t1) {
    			this.pera_x[t1] = head.pera_x[t1];
        		this.pera_y[t1] = head.pera_y[t1];
        		this.pera_z[t1] = head.pera_z[t1];
        		this.perarote_x[t1] = head.perarote_x[t1];
        		this.perarote_y[t1] = head.perarote_y[t1];
        		this.perarote_z[t1] = head.perarote_z[t1];
    		}
    		
    		this.cloud = head.cloud;
    		for(int t1 = 0; t1 < cloud; ++t1) {
    			this.cloud_x[t1] = head.cloud_x[t1];
        		this.cloud_y[t1] = head.cloud_y[t1];
        		this.cloud_z[t1] = head.cloud_z[t1];
    		}
    		
    		this.rader = head.rader;
    		for(int t1 = 0; t1 < rader; ++t1) {
    			this.raderturret[t1] = head.raderturret[t1];
    			this.rader_x[t1] = head.rader_x[t1];
        		this.rader_y[t1] = head.rader_y[t1];
        		this.rader_z[t1] = head.rader_z[t1];
        		this.raderrote_x[t1] = head.raderrote_x[t1];
        		this.raderrote_y[t1] = head.raderrote_y[t1];
        		this.raderrote_z[t1] = head.raderrote_z[t1];
    		}
    		
    		this.exhaust = head.exhaust;
    		this.exhaust_ab = head.exhaust_ab;
    		for(int t1 = 0; t1 < exhaust; ++t1) {
    			this.exhaust_x[t1] = head.exhaust_x[t1];
        		this.exhaust_y[t1] = head.exhaust_y[t1];
        		this.exhaust_z[t1] = head.exhaust_z[t1];
    		}
    		
    		this.laser_sight = head.laser_sight;
    		for(int t1 = 0; t1 < laser_sight; ++t1) {
    			this.rote_laser_sight[t1] = head.rote_laser_sight[t1];
    			this.laser_sight_x[t1] = head.laser_sight_x[t1];
        		this.laser_sight_y[t1] = head.laser_sight_y[t1];
        		this.laser_sight_z[t1] = head.laser_sight_z[t1];
        		this.laser_sight_weapon[t1] = head.laser_sight_weapon[t1];
    		}
    		
    		
    		{
    			this.weapon1true = head.weapon1true;
        		this.weapon1key = head.weapon1key;
        		this.w1name = head.w1name;
        		this.w1missile_aam = head.w1missile_aam;
        		this.ammo1 = head.barst_cycle;
        		this.magazine = head.getMaxDamage();
        		this.reload_time1 = head.reloadtime;
        		this.reloadsoundset1 = head.reloadsoundset1;
        		if(head.reloadsound1 != null)this.reloadsound1 = SoundEvent.REGISTRY.getObject(new ResourceLocation(mod_GVCLib.MOD_ID, head.reloadsound1));
        		this.w1cycle = head.mobcycle;
        		this.w1barst = head.mobbarst;
        		this.weapon1 = head.weapon1;
        		this.w1crossfire = head.w1crossfire;
        		this.w1can_cooldown = head.w1can_cooldown;
        		this.w1kakumax = head.w1kakumax;
        		this.w1kakumin = head.w1kakumin;
        		for(int w1 = 0; w1 < weapon1; ++w1) {
        			this.bullet_type1[w1] = head.bullet_type1[w1];
            		this.bullet_model1[w1] = head.bullet_model;
            		this.bullet_tex1[w1] = head.bullet_tex;
            		this.fire_model1[w1]= head.bulletf_model;
            		this.fire_tex1[w1] = head.bulletf_tex;
            		this.fire_time1[w1] = 2;
            		this.modid = mod_GVCLib.MOD_ID;
            		this.sound_fire1[w1] = head.sound;
            		this.fire_pointx1[w1] = head.fire_pointx1[w1];
            		this.fire_pointy1[w1] = head.fire_pointy1[w1];
            		this.fire_pointz1[w1] = head.fire_pointz1[w1];
            		this.basis_pointx1[w1] = head.basis_pointx1[w1];
            		this.basis_pointy1[w1] = head.basis_pointy1[w1];
            		this.basis_pointz1[w1] = head.basis_pointz1[w1];
            		this.fire_yoffset1[w1] = head.fire_yoffset1[w1];
            		this.rotationfollowing1[w1] = head.rotationfollowing1[w1];
            		this.rotation_player1[w1] = head.rotation_player1[w1];
            		this.rotationfirepoint1[w1] = head.rotationfirepoint1[w1];
            		this.rotation_firepointbxbz1[w1] = head.rotation_firepointbxbz1[w1];
            		this.rotation_lock_pitch_vehicle1[w1] = head.rotation_lock_pitch_vehicle1[w1];
            		this.bullet_damage1[w1] = head.powor;
            		this.bullet_speed1[w1] = head.speed;
            		this.bullet_bure1[w1] = head.bure;
            		this.bullet_expower1[w1] = head.exlevel;
            		if(!mod_GirlFront.cfg_vehicle_breakblock) {
            			this.bullet_ex1[w1] = false;
            		}else {
            			this.bullet_ex1[w1] = head.bullet_ex1[w1];
            		}
            		this.bullet_kazu1[w1] = head.pelletbase;
            		this.bullet_gravity1[w1] = head.gra;
            		this.bullet_livingtime1[w1] = head.bullet_time_max;
            		
        		}
    		
    		
    		}
    		this.mob_w1range = head.mob_w1range;
    		this.mob_w1range_max_y = head.mob_w1range_max_y;
    		this.mob_w1range_min_y = head.mob_w1range_min_y;
    		
    		
    		
    		
    		
    		reset = true;
    	}//item_end
    }
    
    boolean spawn = true;
    
    public void onLivingUpdate() {
		super.onLivingUpdate();
		
		//AI_EntityVehicle.load(this);
		//何が何でも銃は手放さない
				if (this.getRidingEntity() == null) {// 1
					if( this.getItemStackFromSlot(EntityEquipmentSlot.HEAD).isEmpty()) {
						if(!mainitem.isEmpty()) {
							this.setItemStackToSlot(EntityEquipmentSlot.HEAD, mainitem);
						}
					}
				}
		this.readinformation();
		if(this.vehicletype == 3 || this.vehicletype == 5) {
			VehicleCNT_Turret.loadSPG(this, null, false);
		}else {
			this.typeTurret();
		}
		
		this.setVehicleLock(true);
    }
    
    protected void onDeathUpdate() {
 		++this.deathTicks;
 		int x = this.world.rand.nextInt(4);
 		int y = this.world.rand.nextInt(5);
 		int z = this.world.rand.nextInt(4);
 		if (this.deathTicks == 1 && !this.world.isRemote) {
 			GVCExplosionBase.ExplosionKai(this, this, this.posX + 0, this.posY + 0, this.posZ + 0, 3, false,  false);
 		}
 		if (this.deathTicks == 200 && !this.world.isRemote) {
 			this.setDead();
 		}
 	}
    
    public void onUpdate()
    {
        super.onUpdate();
    }
    
   
   /**
    * Determines if an entity can be despawned, used on idle far away entities
    */
   protected boolean canDespawn()
   {
	   return false;
   }
   
}