if isServer() then
    return
end

local function GetTargetEquipmentAmmo(vehicle, Name)
    local HeliType = GetHeliType(vehicle)
    local target = HeliList[HeliType].AmmoType[Name]
    local Count = 0
    for i = 1, HeliList[HeliType].EquipmentMaxCount do
        local RocketTable = vehicle:getPartById(HeliList[HeliType].EquipmentName .. i)
        if RocketTable and RocketTable:getInventoryItem() then
            if RocketTable:getInventoryItem():getType() == target then
                Count = Count + 1
            end
        end

    end
    return Count
end

local function RemoveOneMissile(vehicle, AmmoType)
    local HeliType = GetHeliType(vehicle)
    local target = HeliList[HeliType].AmmoType[AmmoType]
    local Count = 0
    for i = 1, HeliList[HeliType].EquipmentMaxCount do
        local RocketTable = vehicle:getPartById(HeliList[HeliType].EquipmentName .. i)
        if RocketTable and RocketTable:getInventoryItem() then
            if RocketTable:getInventoryItem():getType() == target then
                if target == "RocketNest10" then
                    local CondtionNow = RocketTable:getCondition()
                    if CondtionNow > 0 then
                        RocketTable:setCondition(CondtionNow - 10)
                    else
                        RocketTable:setInventoryItem(nil)
                        RocketTable:setModelVisible(HeliType .. "_" .. HeliList[HeliType].EquipmentName .. i, false)
                    end
                    return
                else
                    RocketTable:setModelVisible(HeliType .. "_" .. HeliList[HeliType].EquipmentName .. i, false)
                    RocketTable:setInventoryItem(nil)
                    return
                end

            end
        end
    end
end

ISHeliRocketAimingCursor = ISBuildingObject:derive("ISHeliRocketAimingCursor")

local this = ISHeliRocketAimingCursor
function ISHeliRocketAimingCursor:create(x, y, z, north, sprite)
    local sq = getWorld():getCell():getGridSquare(x, y, 0)
    self.character:getVehicle():playSound("MissileAway")
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    modData.PanzerReadyToFire = false
    local AmmoType = modData.AmmoTypeNow
    RemoveOneMissile(vehicle, modData.AmmoTypeNow)
    PanzerDetectTarget(sq, modData.AmmoTypeNow)
end
function ISHeliRocketAimingCursor:isValid(square) -- local distan = 10
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    local ApNum = 0
    local HeNum = 0
    if not modData.AmmoTypeNow then
        modData.AmmoTypeNow = "Rocket"
    end
    if GetTargetEquipmentAmmo(vehicle, modData.AmmoTypeNow) <= 0 then
        return false
    end

    return true

end

function ISHeliRocketAimingCursor:render(x, y, z, square)
    if not self.floorSprite then
        self.floorSprite = IsoSprite.new()
        self.floorSprite:LoadFramesNoDirPageSimple('media/ui/FloorTileCursor.png')
    end
    local r, g, b, a = 0.0, 1.0, 0.0, 0.8
    if not self:isValid(square) then
        r = 1.0
        g = 0.0
    end
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    local range = 2
    if modData.AmmoTypeNow == "AnTiTankMissile" then
        range = 5
    end
    for i = -range, range, range * 2 do
        for j = -range, range, range * 2 do
            self.floorSprite:RenderGhostTileColor(x + i, y + j, 0, 1, 1, 0, a)
        end
    end

    self.floorSprite:RenderGhostTileColor(x, y, 0, r, g, b, a)
    -- self.floorSprite:RenderGhostTileColor(x, y, z, r, g, b, a)
    self.square = square
    self.Vaild = self:isValid(square)
end

function ISHeliRocketAimingCursor:new(sprite, northSprite, character, load)
    local o = {}
    setmetatable(o, self)
    self.__index = self
    o:init()
    o:setSprite(sprite)
    o:setNorthSprite(northSprite)
    o.player = 0
    o.character = character
    o.type = "HeliMissile"
    o.load = load
    o.RenderTick = 25
    this.instance = o
    o.noNeedHammer = true
    o.skipBuildAction = true
    o.maxrange = 100
    o.square = nil
    o.FireTick = 1
    o.Vaild = false
    o.skipWalk2 = true
    o.canBeAlwaysPlaced = true
    return o
end
