require "ISUI/ISPanel"
require('ISUI/ISScrollingListBox')
local FONT_HGT_SMALL = getTextManager():getFontHeight(UIFont.NewSmall)
local FONT_HGT_MEDIUM = getTextManager():getFontHeight(UIFont.NewMedium)
local FONT_HGT_LARGE = getTextManager():getFontHeight(UIFont.NewLarge)
PanzerPlayerGetPointsUI = ISPanel:derive("PanzerPlayerGetPointsUI");
local this = PanzerPlayerGetPointsUI

local btnPanzerPlayerGetPointsUI = getText("IGUI_ABFP_Buy")
local btnClose = getText("IGUI_CraftUI_Close")
local btnCompleteWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnPanzerPlayerGetPointsUI) + 10
local btnCompleteHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnPanzerPlayerGetPointsUI) + 10
local btnCloseWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnClose) + 10
local btnCloseHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnClose) + 10
function PanzerPlayerGetPointsUI:initialise()
    ISPanel.initialise(self);
    self:create();
end

function PanzerPlayerGetPointsUI:prerender()
    ISPanel.prerender(self);
    self.drawText(self, getText("IGUI_ABFP_YourPoint"),
        self.width - (getTextManager():MeasureStringX(UIFont.NewSmall, getText("IGUI_ABFP_YourPoint"))) - 10, 10, 1, 1,
        1, 1, UIFont.NewSmall);
    local ModData = self.character:getModData()
    if ModData.ATPoint then
        self.drawText(self, tostring(ModData.ATPoint),
            self.width - (getTextManager():MeasureStringX(UIFont.NewSmall, tostring(ModData.ATPoint))) - 10,
            10 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall);
    else
        self.drawText(self, "0", self.width - (getTextManager():MeasureStringX(UIFont.NewSmall, "0")) - 10,
            10 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall);
    end
end

function PanzerPlayerGetPointsUI:destroy()
    UIManager.setShowPausedMessage(true)
    self:setVisible(false)
    self:removeFromUIManager()
end

local Ammo = {{
    Type = "Base.APAmmo",
    Money = 50
}, {
    Type = "Base.APFSDSAmmo",
    Money = 75
}, {
    Type = "Base.HEAmmo",
    Money = 100
}, {
    Type = "Base.HEATAmmo",
    Money = 100
}, {
    Type = "Base.792Box",
    Money = 20
}, {
    Type = "Base.30mmAP",
    Money = 25
}, {
    Type = "Base.30mmHE",
    Money = 40
}, {
    Type = "Base.Rocket10",
    Money = 100
}, {
    Type = "Base.AnTiTankMissile10",
    Money = 150
}, {
    Type = "Base.MachineGun10",
    Money = 1000
}, {
    Type = "Base.RocketNest10",
    Money = 500
}, {
    Type = "Base.HeliGun10",
    Money = 0
}}

function PanzerPlayerGetPointsUI:update()
    ISPanel.update(self)
end

local Tick = 0
local RenderTick = 5
function PanzerPlayerGetPointsUI:render()

end

function PanzerPlayerGetPointsUI:create()

    self.Buy = ISButton:new(self.width - btnCompleteWid - 5, self.height - btnCompleteHgt - 5, btnCompleteWid,
        btnCompleteHgt, btnPanzerPlayerGetPointsUI, self, this.onOptionMouseDown);
    self.Buy.internal = "Buy";
    self.Buy:initialise();
    self.Buy:instantiate();
    self.Buy.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.1
    }
    self:addChild(self.Buy);

    self.close = ISButton:new(self.width - btnCompleteWid - btnCompleteWid - btnCloseWid,
        self.height - btnCompleteHgt - 5, btnCompleteWid, btnCompleteHgt, btnClose, self, this.onOptionMouseDown);
    self.close.internal = "Close";
    self.close:initialise();
    self.close:instantiate();

    self.close.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.1
    }
    self:addChild(self.close);

    self.scrollingListItem = ISScrollingListBox:new(10, 20, 200, self.height - 25 - btnCompleteHgt)
    self.scrollingListItem.anchorRight = true
    self.scrollingListItem.anchorBottom = true
    self.scrollingListItem.itemheight = FONT_HGT_SMALL * 2
    self.scrollingListItem:initialise();
    self.scrollingListItem:instantiate();
    self.scrollingListItem.selected = 0
    self.scrollingListItem.joypadParent = self;
    self.scrollingListItem.font = UIFont.NewMedium;
    self.scrollingListItem.doDrawItem = self.drawItem
    self.scrollingListItem.drawBorder = true;
    self:addChild(self.scrollingListItem);
    self:initList()

    self.textEntry = ISTextEntryBox:new("1", self.width - btnCompleteWid, self.height - 3 * btnCompleteHgt - 5,
        btnCompleteHgt, 10, 10, 10);
    self.textEntry.font = UIFont.Medium
    self.textEntry:initialise();
    self.textEntry:instantiate();
    self.textEntry.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0.5
    };
    self.textEntry.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.0
    };
    self.textEntry:setHasFrame(true)
    self.textEntry:setAnchorTop(false);
    self.textEntry:setAnchorBottom(true);
    self.textEntry:setAnchorRight(true);
    self:addChild(self.textEntry)
end

local function Copy_Table(tab)
    local ret = {}
    for k, v in pairs(tab) do
        if type(v) == "table" then
            ret[k] = Copy_Table(v)
        else
            ret[k] = v
        end
    end
    return ret
end

function PanzerPlayerGetPointsUI:initList()
    local CategoryList = Ammo
    local NewTable = Copy_Table(CategoryList)
    for k, v in pairs(NewTable) do
        v.Money = v.Money * SandboxVars.WarThunder.AmmoPrice
        self.scrollingListItem:addItem(k, v)
    end
end

local function DeletePlayerHABFPoney(Item, Num, playerObj)
    local ModData = playerObj:getModData()
    if ModData.ATPoint then
        if ModData.ATPoint >= Item.Money * Num then
            ModData.ATPoint = ModData.ATPoint - Item.Money * Num
            return true
        else
            playerObj:Say(getText("IGUI_ABFP_NoEnoughPoint"))
            return false
        end
    else
        playerObj:Say(getText("IGUI_ABFP_NoEnoughPoint"))
        return false
    end
end

function PanzerPlayerGetPointsUI:onOptionMouseDown(button, x, y)
    if button.internal == "Buy" then
        if self.scrollingListItem.selected then
            if self.scrollingListItem.items then
                local Item = self.scrollingListItem.items[self.scrollingListItem.selected]
                if Item then
                    Item = Item.item
                    local Num = tonumber(self.textEntry:getText())
                    if not Num then
                        Num = 1
                        self.character:Say(getText("IGUI_ABFP_WrongNumber"))
                    end
                    if DeletePlayerHABFPoney(Item, Num, self.character) then
                        self.character:getInventory():AddItems(Item.Type, Num)
                        local itemName = instanceItem(Item.Type):getDisplayName()
                        self.character:Say(getText("IGUI_ABFP_BuyItem") .. itemName .. "*" .. Num)
                    end
                else
                    self.character:Say(getText("IGUI_ABFP_NoItemSelected"))
                end
            end
        end
    end
    if button.internal == "Close" then
        self:destroy()
    end
end

function PanzerPlayerGetPointsUI:drawItem(_y, _item, _alt)
    if _y + self:getYScroll() + self.itemheight < 0 or _y + self:getYScroll() >= self.height then
        return _y + self.itemheight
    end
    local unit = _item.item
    if self.selected == _item.index then
        self:drawRect(0, (_y), self:getWidth(), self.itemheight, 0.3, 1, 1, 0)
    end
    local Item = instanceItem(unit.Type)
    local ItemName = "1 * " .. Item:getDisplayName()
    local ItemNameW = getTextManager():MeasureStringX(UIFont.NewSmall, ItemName)
    local ItemTexture = Item:getTexture()
    self:drawRectBorder(0, (_y), self:getWidth(), self.itemheight, 0.5, 1, 1, 1)
    self:drawText(ItemName, 50, _y + FONT_HGT_SMALL, 0, 1, 0, 1, UIFont.NewSmall)
    self:drawTextureScaledAspect(ItemTexture, 0, _y + 5, 32, 32, 2, 2, 2, 2)
    local PointsNeed = unit.Money
    if PointsNeed then
        local PointsNeedW = getTextManager():MeasureStringX(UIFont.NewSmall, tostring(PointsNeed))
        self:drawText(tostring(PointsNeed), 50, _y, 0, 1, 0, 1, UIFont.NewSmall)
    end

    return _y + self.itemheight
end

function PanzerPlayerGetPointsUI:new(x, y, width, height, player)
    local o = {};
    o = ISPanel:new(x, y, width, height);
    setmetatable(o, self);
    o.character = player;
    self.__index = self;
    this.instance = o
    o.RankLevel = 1
    o.Category = nil
    o.LastCategory = nil
    o.ItemCategory = nil
    o.LastItemCategory = nil
    o.moveWithMouse = true;
    o.Item = nil
    o.anchorRight = true
    o.anchorBottom = true
    o.anchorTop = true
    o.anchorLeft = true
    return o;
end

