package gtanks.battles.tanks.weapons.shaft;

import gtanks.RandomUtils;
import gtanks.battles.BattlefieldModel;
import gtanks.battles.BattlefieldPlayerController;
import gtanks.battles.tanks.weapons.IEntity;
import gtanks.battles.tanks.weapons.IWeapon;
import gtanks.battles.tanks.weapons.WeaponWeakeningData;
import gtanks.commands.Type;
import gtanks.logger.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ShaftModel implements IWeapon {
   private BattlefieldModel bfModel;
   private BattlefieldPlayerController player;
   private ShaftEntity entity;
   private WeaponWeakeningData weakeingData;

   public ShaftModel(ShaftEntity entity, WeaponWeakeningData weakeingData, BattlefieldModel bfModel, BattlefieldPlayerController player) {
      this.entity = entity;
      this.bfModel = bfModel;
      this.player = player;
      this.weakeingData = weakeingData;
   }
   public long timeEnd =0L,timeStart=0L;

   public void fire(String json) {
      //Logger.debug("obr"+json);
      JSONParser js = new JSONParser();
      JSONObject jo = null;

      try {
         jo = (JSONObject)js.parse(json);
      } catch (ParseException var5) {
         var5.printStackTrace();
      }
      long par = 0L;
      if(jo.get("energy")!= null){
         timeEnd = System.currentTimeMillis();
         if(timeEnd-timeStart<=500L){}
         else {
            par = timeEnd-timeStart;
            if(par>3500)par=3500;

         }

      }
      Logger.log(""+par);

      //if(!this.check((int)((Long)jo.get("reloadTime")).longValue())) {
      //this.bfModel.cheatDetected(this.player, this.getClass());
      //} else {
      JSONArray array = (JSONArray)jo.get("targets");
      Logger.log(""+array.size());
      BattlefieldPlayerController[] victim = new BattlefieldPlayerController[array.size()];
      // JSONArray array = (JSONArray)jo.get("targets");
      for(int tt = 0;tt<array.size();tt++){
         JSONObject jsonA = (JSONObject)array.get(tt);
         JSONObject jsonb = (JSONObject)jsonA.get("target");
         // Logger.log("json;"+jsonA.toJSONString());
         BattlefieldPlayerController v = (BattlefieldPlayerController) this.bfModel.players.get((String)jsonb.get("id"));
         Logger.log(""+jsonb.get("id"));
         Logger.log(v.tank.id);
         victim[tt] = v;
      }
      if(par>0L && par<=3500L) {
         //this.bfModel.fire(this.player, json);
         // BattlefieldPlayerController victim = (BattlefieldPlayerController) this.bfModel.players.get(jo.get("victimId"));
         if (victim != null) {
            this.onTarget1(victim,10000, par);
         }
      }
      //else {
      // this.bfModel.fire(this.player, json);

      // BattlefieldPlayerController victim = (BattlefieldPlayerController) this.bfModel.players.get(jo.get("victimId"));
      // if (victim != null) {
      //    this.onTarget(new BattlefieldPlayerController[]{victim}, (int) Double.parseDouble(String.valueOf(jo.get("distance"))));
      // }
//}
      //}
   }


   public void startFire(String json) {
      Logger.debug(json);
      timeStart=System.currentTimeMillis();
      this.bfModel.sendToAllPlayers(this.player, Type.BATTLE, "start_fire", this.player.tank.id, json);

   }

   public void stopFire() {
      timeEnd = System.currentTimeMillis();
      //    this.bfModel.stopFire(this.player);

   }

   public void onTarget(BattlefieldPlayerController[] targetsTanks, int distance) {
      if(targetsTanks.length != 0) {
         if(targetsTanks.length > 1) {
            Logger.log("SmokyModel::onTarget() Warning! targetsTanks length = " + targetsTanks.length);
         }

         float damage = RandomUtils.getRandom(this.entity.damage_min, this.entity.damage_max);

         for(int i = 0; i < targetsTanks.length; ++i) {
            this.bfModel.tanksKillModel.damageTank(targetsTanks[i], this.player, damage, true);
            damage /= 2.0F;
         }
      }
   }

   public void onTarget1(BattlefieldPlayerController[] targetsTanks, double rel,long l) {
      if(targetsTanks.length != 0) {


         // float damage = entity.damage_max*3.5f;
         float damage = (float) (rel/3500*this.entity.damage_max);
         Logger.log(""+damage);
         for(int i = 0; i < targetsTanks.length; ++i) {
            this.bfModel.tanksKillModel.damageTank(targetsTanks[i], this.player, damage, false);
            damage /= 1.2F;
         }
      }
   }
   public IEntity getEntity() {
      return this.entity;
   }

   public void effect() {

   }
}
как исправить чтобы у пушки shaft появился балон, а то он вечно пустой и стрелять нельзя