if isServer() then
    return
end

ISHeliAimingCursor = ISBuildingObject:derive("ISHeliAimingCursor")
ISHeliAimTime = 0
local this = ISHeliAimingCursor
function ISHeliAimingCursor:create(x, y, z, north, sprite)

end
function ISHeliAimingCursor:isValid(square) -- local distan = 10
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    local ApNum = 0
    local HeNum = 0
    local Type = GetHeliType(vehicle)
    local AmmoBox = vehicle:getPartById(Type .. "_AmmoCache")
    local AmmoNeed = HeliList[Type].AmmoType[modData.AmmoTypeNow]
    if AmmoBox then
        local inv = AmmoBox:getItemContainer()
        local AmmoCount = inv:getNumberOfItem(AmmoNeed)
        if AmmoCount <= 0 then
            return false
        end
    end
    return true
end

function ISHeliAimingCursor: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()
    if modData.AmmoTypeNow == "HE" then
        for i = -2, 2, 4 do
            for j = -2, 2, 4 do
                self.floorSprite:RenderGhostTileColor(x + i, y + j, 0, 1, 1, 0, a)
            end
        end
    elseif modData.AmmoTypeNow == "AP" then
        for i = -1, 1, 2 do
            for j = -1, 1, 2 do
                self.floorSprite:RenderGhostTileColor(x + i, y + j, 0, 1, 1, 0, a)
            end
        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 ISHeliAimingCursor: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 = "Heli"
    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

local function HeliMGFire(key)
    local playerObj = getPlayer()
    local inv = playerObj:getInventory()
    local vehicle = playerObj:getVehicle()
    if (vehicle and GetHeliType(vehicle)) and vehicle:getSeat(playerObj) == 0 then
        if this and this.instance then
            if isMouseButtonDown(1) and key == getKeyCode("G") then
                if this.instance.Vaild then
                    if this.instance.FireTick > 0 then
                        this.instance.FireTick = this.instance.FireTick - 1
                        return
                    else
                        this.instance.FireTick = 5 / (60 / getAverageFPS())
                    end
                    local Type = GetHeliType(vehicle)
                    playerObj:getVehicle():playSound(HeliList[Type].FireSound)
                    local vehicle = playerObj:getVehicle()
                    local modData = vehicle:getModData()
                    local AmmoType = modData.AmmoTypeNow
                    local AmmoBox = vehicle:getPartById(Type .. "_AmmoCache")
                    if AmmoBox then
                        local inv = AmmoBox:getItemContainer()
                        local AmmoNeed = HeliList[Type].AmmoType[modData.AmmoTypeNow]
                        local Ammo = inv:FindAndReturn(AmmoNeed)
                        inv:Remove(Ammo)
                        inv:removeItemOnServer(Ammo)
                    end
                    local Gun = vehicle:getPartById(Type .. "_Gun")
                    vehicle:playPartAnim(Gun, "Open")
                    local sq = getWorld():getCell():getGridSquare(this.instance.square:getX(),
                        this.instance.square:getY(), 0)
                    PanzerDetectTarget(sq, AmmoType)
                end
            end
        end
    end
end

local function CheckLocal()
    Events.OnKeyStartPressed.Add(HeliMGFire);
    Events.OnKeyPressed.Add(HeliMGFire);
    Events.OnKeyKeepPressed.Add(HeliMGFire);
end
Events.OnGameStart.Add(CheckLocal)
