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)
TurretAmmoBookUI = ISPanel:derive("TurretAmmoBookUI");
local this = TurretAmmoBookUI

local btnTurretAmmoBookUI = getText("IGUI_ASM_Buy")
local btnClose = getText("IGUI_CraftUI_Close")
local btnNext = getText("IGUI_CraftUI_Next")
local btnPrev = getText("IGUI_CraftUI_Prev")

local btnCompleteWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnTurretAmmoBookUI) + 10
local btnCompleteHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnTurretAmmoBookUI) + 10
local btnCloseWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnClose) + 10
local btnCloseHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnClose) + 10
function TurretAmmoBookUI:initialise()
    ISPanel.initialise(self);
    self:create();
end

function TurretAmmoBookUI:prerender()
    ISPanel.prerender(self);
    local TextPath = "media/textures/UI/BackGroundBook.png"
    local Tex = getTexture(TextPath)
    self:drawTextureScaledAspect(Tex, 0, 0, self.width + 20, self.height + 20, 1, 1, 1, 1)
end

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

function TurretAmmoBookUI:render()
    local lang = Translator.getLanguage():name();
    local LeftPngIndex = self.PngNow
    local RightPngIndex = self.PngNow + 1
    if getTexture("media/textures/UI/" .. lang .. "/TurretAmmoBookUI/1.png") == nil then
        lang = "EN"
    end
    local LeftTexPath = "media/textures/UI/" .. lang .. "/TurretAmmoBookUI/" .. LeftPngIndex .. ".png"
    local RightTexPath = "media/textures/UI/" .. lang .. "/TurretAmmoBookUI/" .. RightPngIndex .. ".png"
    local LeftTex = getTexture(LeftTexPath)
    local RightTex = getTexture(RightTexPath)
    if LeftTex ~= nil then
        self:drawTextureScaledAspect(LeftTex, 10, 128, 420, 650, 1, 1, 1, 1)
    end
    if RightTex ~= nil then
        self:drawTextureScaledAspect(RightTex, self.width - 10 - 400, 128, 420, 650, 1, 1, 1, 1)
    end
    self.close:bringToTop()
end

function TurretAmmoBookUI:create()

    self.close = ISButton:new(self.width - 64 - 5, 128, 64, 64, nil, self, this.onOptionMouseDown);
    self.close.internal = "Close";
    self.close:initialise();
    self.close:instantiate();
    self.close.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    };
    self.close.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0
    }
    self.close:setImage(getTexture("media/textures/UI/close.png"));
    self:addChild(self.close);

    self.NextPng = ISButton:new(self.width - 64 - 5, self.height - 5 - 150, 64, 28, nil, self, this.onOptionMouseDown);
    self.NextPng.internal = "NextPng";
    self.NextPng:initialise();
    self.NextPng:instantiate();

    self.NextPng.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0
    }
    self.NextPng.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    };
    self.NextPng:setImage(getTexture("media/textures/UI/Right.png"));
    self:addChild(self.NextPng);

    self.PrevPng = ISButton:new(5, self.height - 5 - 150, 64, 28, nil, self, this.onOptionMouseDown);
    self.PrevPng.internal = "PrevPng";
    self.PrevPng:initialise();
    self.PrevPng:instantiate();
    self.PrevPng.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    };
    self.PrevPng.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0
    }
    self.PrevPng:setImage(getTexture("media/textures/UI/Left.png"));
    self:addChild(self.PrevPng);

end

function TurretAmmoBookUI:onOptionMouseDown(button, x, y)

    if button.internal == "Close" then
        self:destroy()
    end
    if button.internal == "NextPng" then
        self.PngNow = self.PngNow + 2
        if self.PngNow > 3 then
            self.PngNow = 1
        end
    end
    if button.internal == "PrevPng" then
        self.PngNow = self.PngNow - 2
        if self.PngNow < 1 then
            self.PngNow = 3
        end
    end
end

function TurretAmmoBookUI: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.PngNow = 1
    o.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    };
    o.borderColor = {
        r = 0.9,
        g = 0.9,
        b = 0.9,
        a = 0
    };
    o.moveWithMouse = true;
    o.anchorRight = true
    o.anchorBottom = true
    o.anchorTop = true
    o.anchorLeft = true
    return o;
end

local function OpenHelpLabUI(player)
    local maxY = getCore():getScreenHeight()
    local maxX = getCore():getScreenWidth()
    local width = 900
    local height = 900
    local x = (maxX - width) / 2
    local y = (maxY - height) / 2
    if TurretAmmoBookUI.instance then
        TurretAmmoBookUI.instance:destroy()
    end
    local TurretAmmoBookUI = TurretAmmoBookUI:new(x, y, width, height, player);
    TurretAmmoBookUI:addToUIManager();
    TurretAmmoBookUI:initialise();

end

local function OpenBook(_player, _context, _items) -- 设置你的物品的右键菜单来打开窗口
    local container = nil
    local resItems = {}
    for i, v in ipairs(_items) do
        if not instanceof(v, "InventoryItem") then
            for _, it in ipairs(v.items) do
                resItems[it] = true
            end
            container = v.items[1]:getContainer()
        else
            resItems[v] = true
            container = v:getContainer()
        end
    end
    local square = getPlayer():getCurrentSquare()
    for v, _ in pairs(resItems) do
        if v:getType() == "TurretAmmoBook" then
            local NewItem = instanceItem("AutoTurret.TurretAmmoBook")
            _context:addOption(getText("IGUI_Open") .. NewItem:getDisplayName(), getPlayer(), OpenHelpLabUI)
            return
        end
    end
end

Events.OnFillInventoryObjectContextMenu.Add(OpenBook)
