if isServer() then
    return
end

ISPanzerMG34AimingCursor = ISBuildingObject:derive("ISPanzerMG34AimingCursor")
local this = ISPanzerMG34AimingCursor
function ISPanzerMG34AimingCursor:create(x, y, z, north, sprite)
    return
end

function ISPanzerMG34AimingCursor:IsPanzerIIMGReadyToFire()
    local inv = self.character:getInventory()
    local Type = GetPanzerType(self.character:getVehicle())
    local modData = self.character:getVehicle():getModData()
    local AmmoType = modData.AmmoTypeNow
    if Type == "Wirbelwind" then

        if AmmoType == "AP" then
            if inv:FindAndReturn("30mmAP") then
                return true
            else
                return false
            end
            if AmmoType == "HE" then
                if inv:FindAndReturn("30mmHE") then
                    return true
                else
                    return false
                end
            end
        end
    end
    if AmmoType == "MG" then
        local SearchAmmoType = PanzerList[Type].AmmoType["MG"]
        if inv:FindAndReturn(SearchAmmoType) then
            return true
        else
            return false
        end
    end
    return false
end

function ISPanzerMG34AimingCursor:isValid(square) -- local distan = 10
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    if not self.character:getModData().PanzerReadyToFire then
        return false
    end
    if isMouseButtonDown(1) and self:IsPanzerIIMGReadyToFire() then
        local distan = ((square:getX() - self.character:getX()) ^ 2 + (square:getY() - self.character:getY()) ^ 2) ^ 0.5
        if distan > 5 then
            return true

        else
            return false
        end
    else
        return false
    end

end

-- function ISPanzerMG34AimingCursor:walkTo(x, y, z)

--     return
-- end

function ISPanzerMG34AimingCursor: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
    for i = -2, 2, 4 do
        for j = -2, 2, 4 do
            self.floorSprite:RenderGhostTileColor(x + i, y + j, z, 1, 1, 0, a)
        end
    end

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

end

function ISPanzerMG34AimingCursor:new(sprite, northSprite, character)
    local o = {}
    setmetatable(o, self)
    self.__index = self
    this.instance = o
    o:init()
    o:setSprite(sprite)
    o:setNorthSprite(northSprite)
    o.player = 0
    o.character = character
    o.FireTick = 10
    o.type = "PanzerMG"

    o.noNeedHammer = true
    o.skipBuildAction = true
    o.maxrange = 20
    o.square = nil
    o.Vaild = false
    o.skipWalk2 = true
    o.canBeAlwaysPlaced = true
    return o
end

ISPanzerMG42AimingCursor = ISBuildingObject:derive("ISPanzerMG34AimingCursor")
local this2 = ISPanzerMG42AimingCursor
function ISPanzerMG42AimingCursor:create(x, y, z, north, sprite)
    return
end

function ISPanzerMG42AimingCursor:IsPanzerIIMGReadyToFire()
    local inv = self.character:getInventory()
    local Vehicle = self.character:getVehicle()
    local MGAmmoType = PanzerList[GetPanzerType(Vehicle)].AmmoType["MG"]
    if inv:FindAndReturn(MGAmmoType) then
        return true
    else
        return false
    end
end

function ISPanzerMG42AimingCursor:isValid(square) -- local distan = 10
    local vehicle = self.character:getVehicle()
    local modData = vehicle:getModData()
    if isMouseButtonDown(1) and self:IsPanzerIIMGReadyToFire() then
        local distan = ((square:getX() - self.character:getX()) ^ 2 + (square:getY() - self.character:getY()) ^ 2) ^ 0.5
        if distan > 5 then
            return true

        else
            return false
        end
    else
        return false
    end

end

-- function ISPanzerMG34AimingCursor:walkTo(x, y, z)

--     return
-- end

function ISPanzerMG42AimingCursor: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
    for i = -2, 2, 4 do
        for j = -2, 2, 4 do
            self.floorSprite:RenderGhostTileColor(x + i, y + j, z, 1, 1, 0, a)
        end
    end

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

function ISPanzerMG42AimingCursor:new(sprite, northSprite, character)
    local o = {}
    setmetatable(o, self)
    self.__index = self
    this2.instance = o
    o:init()
    o:setSprite(sprite)
    o:setNorthSprite(northSprite)
    o.player = 0
    o.character = character
    o.FireTick = 6
    o.type = "PanzerMG"
    o.noNeedHammer = true
    o.skipBuildAction = true
    o.maxrange = 20
    o.square = nil
    o.Vaild = false
    o.skipWalk2 = true
    o.canBeAlwaysPlaced = true
    return o
end

local function RemoveAmmo(vehicle, AmmoType)
    local AmmoBox = vehicle:getPartById("Wirbelwind_AmmoCache")
    if AmmoBox then
        local inv = AmmoBox:getItemContainer()
        local Ammo = inv:FindAndReturn(AmmoType)
        if Ammo then
            inv:Remove(Ammo)
            inv:removeItemOnServer(Ammo)
            return
        end
    end
    local modData = vehicle:getModData()
    if modData.researchTable and modData.researchTable["BigAmmoCache"] then
        local AmmoBox = vehicle:getPartById("Wirbelwind_AmmoCacheBig")
        if AmmoBox then
            local inv = AmmoBox:getItemContainer()
            local Ammo = inv:FindAndReturn(AmmoType)
            if Ammo then
                inv:Remove(Ammo)
                inv:removeItemOnServer(Ammo)
                return
            end
        end
    end
end
local function PanzerIIMGFire(key)
    local playerObj = getPlayer()
    local inv = playerObj:getInventory()
    local vehicle = playerObj:getVehicle()
    if (vehicle and GetPanzerType(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 = 10 - (5 * GetAbilityLevel(playerObj:getVehicle(), "FastShootMG") / 10)
                    end
                    if GetPanzerType(vehicle) == "Wirbelwind" then
                        local modData = vehicle:getModData()
                        local AmmoType = modData.AmmoTypeNow
                        playerObj:getVehicle():playSound("EC665_30mm_Fire")
                        if AmmoType == "AP" then
                            RemoveAmmo(vehicle, "30mmAP")
                        end
                        if AmmoType == "HE" then
                            RemoveAmmo(vehicle, "30mmHE")
                        end
                        PanzerDetectTarget(this.instance.square, AmmoType)
                        return
                    end
                    playerObj:getVehicle():playSound("MG34_Fire")
                    local MGAmmoType = PanzerList[GetPanzerType(vehicle)].AmmoType["MG"]
                    playerObj:getInventory():RemoveOneOf(MGAmmoType)

                    local vehicle = playerObj:getVehicle()
                    PanzerDetectTarget(this.instance.square, "MG")
                end
            end
        end
    end
    if (vehicle and GetPanzerType(vehicle)) and vehicle:getSeat(playerObj) == 2 then
        if this2 and this2.instance then
            if isMouseButtonDown(1) and key == getKeyCode("G") then
                if this2.instance.Vaild then
                    if this2.instance.FireTick > 0 then
                        this2.instance.FireTick = this2.instance.FireTick - 1
                        return
                    else
                        this2.instance.FireTick = 6 - (5 * GetAbilityLevel(playerObj:getVehicle(), "FastShootMG") / 10)
                    end
                    local sq = getWorld():getCell():getGridSquare(this2.instance.square:getX(),
                        this2.instance.square:getY(), this2.instance.square:getZ())
                    if GetPanzerType(vehicle) == "99A" then
                        playerObj:getVehicle():playSound("99A_Laser_Sound")
                        PanzerDetectTarget(sq, "MGLaser")
                        return
                    end
                    playerObj:getVehicle():playSound("MG42_Fire")
                    local MGAmmoType = PanzerList[GetPanzerType(vehicle)].AmmoType["MG"]
                    playerObj:getInventory():RemoveOneOf(MGAmmoType)
                    PanzerDetectTarget(sq, "MG")
                end
            end
        end
    end
end

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