package com.lothrazar.cyclicmagic.projectile;

import java.util.ArrayList;
import com.lothrazar.cyclicmagic.Const;
import com.lothrazar.cyclicmagic.util.UtilParticle;
import com.lothrazar.cyclicmagic.util.UtilSound;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.util.BlockPos;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

public class EntityWaterBolt extends EntityThrowable {
	ArrayList<Block> waterBoth = new ArrayList<Block>();
	public EntityWaterBolt(World worldIn) {
		super(worldIn);
		waterBoth.add(Blocks.flowing_water);
		waterBoth.add(Blocks.water);
	}

	public EntityWaterBolt(World worldIn, EntityLivingBase ent) {
		super(worldIn, ent);
	}

	public EntityWaterBolt(World worldIn, double x, double y, double z) {
		super(worldIn, x, y, z);
	}

	public static final String name = "waterbolt";
	public static Item item = null;

	@Override
	protected void onImpact(MovingObjectPosition mop) {

		if (mop.entityHit != null) {
			// do the snowball damage, which should be none. put out the fire

			if (mop.entityHit instanceof EntityLivingBase) {
				mop.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), 0);

				UtilParticle.spawnParticle(this.worldObj, EnumParticleTypes.WATER_SPLASH, mop.entityHit.getPosition());

				EntityLivingBase e = (EntityLivingBase) mop.entityHit;

				if (e.isBurning()) {
					e.extinguish();
				}
			}
		}

		BlockPos pos = mop.getBlockPos();
		if(pos == null){
			pos = this.getPosition();
		}

		if(pos != null){
			UtilParticle.spawnParticle(this.worldObj, EnumParticleTypes.WATER_SPLASH, pos);
			
			if (this.getThrower() instanceof EntityPlayer && mop.sideHit != null
					&& this.worldObj.isRemote == false) {
				this.worldObj.extinguishFire((EntityPlayer) this.getThrower(), pos, mop.sideHit);
			}
		}
		
		if (this.dimension == Const.Dimension.nether){
			UtilSound.playSoundAt(this, UtilSound.fizz);
		}
		else {
			UtilSound.playSoundAt(this, UtilSound.splash);
			
			//so far its both client and server
			if(this.worldObj.isRemote == false){
				System.out.println("TESTING TIME " + this.worldObj.isRemote);
				System.out.println(mop.toString());
				System.out.println(mop.getBlockPos());
				System.out.println("isAirOrWater "  + this.isAirOrWater(pos));
				System.out.println("=====");
				
				if(pos != null){
					System.out.println(this.worldObj.getBlockState(pos).getBlock().getUnlocalizedName());
					if (this.isAirOrWater(pos)){
						System.out.println("set water");
						this.worldObj.setBlockState(pos, Blocks.flowing_water.getDefaultState(),3);
					}
					if(mop.sideHit != null){
						BlockPos offset = pos.offset(mop.sideHit);
						System.out.println("offset "  + this.isAirOrWater(offset));
						System.out.println(this.worldObj.getBlockState(offset).getBlock().getUnlocalizedName());
						if (offset != null && this.isAirOrWater(offset)) {
					
							System.out.println("offset water "+ this.worldObj.isRemote );
			 
							this.worldObj.setBlockState(offset, Blocks.flowing_water.getDefaultState(),3);
						}
					}
				}
			}
		}

		this.setDead();
	}
	
	private boolean isAirOrWater(BlockPos pos){
		if(pos == null){
			return false;
		}
		return this.worldObj.isAirBlock(pos) || 
				this.worldObj.getBlockState(pos).getBlock().getUnlocalizedName().equalsIgnoreCase("tile.water") || 
				(this.worldObj.getBlockState(pos) != null && waterBoth.contains(this.worldObj.getBlockState(pos).getBlock()));
	}
}
@@ -2065,24 +2065,70 @@
 == false) %7B%0A
+%09%09%09%09//TODO: could extinguish in a bigger area%0A
 %09%09%09%09this.wor
@@ -2461,372 +2461,34 @@
 )%7B%0A%09
-%09%09%09System.out.println(%22TESTING TIME %22 + this.worldObj.isRemote);%0A%09%09%09%09System.out.println(mop.toString());%0A%09%09%09%09System.out.println(mop.getBlockPos());%0A%09%09%09%09System.out.println(%22isAirOrWater %22  + this.isAirOrWater(pos));%0A%09%09%09%09System.out.println(%22=====%22);%0A%09%09%09%09%0A%09%09%09%09if(pos != null)%7B%0A%09%09%09%09%09System.out.println(this.worldObj.getBlockState(pos).getBlock().getUnlocalizedName());
+%0A%09%09%09%09if(pos != null)%7B%0A%09%09%09 
 %0A%09%09%09
@@ -2526,42 +2526,8 @@
 %09%09%09%09
-%09%09System.out.println(%22set water%22);
 %0A%09%09%09
@@ -2697,299 +2697,68 @@
 ;%0A%09%09
-%09%09%09%09System.out.println(%22offset %22  + this.isAirOrWater(offset));%0A%09%09%09%09%09%09System.out.println(this.worldObj.getBlockState(offset).getBlock().getUnlocalizedName());%0A%09%09%09%09%09%09if (offset != null && this.isAirOrWater(offset)) %7B%0A%09%09%09%09%09%0A%09%09%09%09%09%09%09System.out.println(%22offset water %22+ this.worldObj.isRemote );%0A
+%0A%09%09%09%09%09%09if (offset != null && this.isAirOrWater(offset)) %7B%0A%09%09
 %09%09%09 

remove water logspam from testing