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

local btnClose = getText("IGUI_CraftUI_Close")
local btnUpload = getText("IGUI_ISPanzerCrewLevel_Equip")
local btnCloseWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnClose) + 10
local btnCloseHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnClose) + 10
local btnUploadWid = getTextManager():MeasureStringX(UIFont.NewSmall, btnUpload) + 10
local btnUploadHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnUpload) + 10
function ISPanzerCrewLevel:initialise()
    ISPanel.initialise(self);
    self:create();
end

function ISPanzerCrewLevel:prerender()
    ISPanel.prerender(self);

    self:drawTextureScaled(getTexture("media/textures/UI/BackGround.png"), 33, 15, 155, 48, 1, 1, 1, 1)
    self:drawText(getText("IGUI_PanzerCrew_Now"), 33 + 5, 48 + 15 + 5, 1, 1, 1, 1, UIFont.NewMedium);
    self:drawText(getText("IGUI_PanzerCrewCommander"), 33 + 5, 48 + 15 + 5 + FONT_HGT_MEDIUM, 0, 1, 0, 1,
        UIFont.NewSmall);
    self:drawText(getText("IGUI_PanzerCrewGunner"), 33 + 5, 48 + 15 + 5 + FONT_HGT_MEDIUM * 2, 0, 1, 0, 1,
        UIFont.NewSmall);
    self:drawText(getText("IGUI_PanzerCrewDriver"), 33 + 5, 48 + 15 + 5 + FONT_HGT_MEDIUM * 3, 0, 1, 0, 1,
        UIFont.NewSmall);
    self:drawText(getText("IGUI_PanzerCrewLoader"), 33 + 5, 48 + 15 + 5 + FONT_HGT_MEDIUM * 4, 0, 1, 0, 1,
        UIFont.NewSmall);
    local modData = self.vehicle:getModData()
    if not (modData.researchTable and modData.researchTable["AutoMG"]) then
        self:drawText(getText("IGUI_PanzerCrewMachineGunner"), 33 + 5, 48 + 15 + 5 + FONT_HGT_MEDIUM * 5, 1, 0, 0, 1,
            UIFont.NewSmall);
    else
        self:drawText(getText("IGUI_PanzerCrewMachineGunner"), 33 + 5, 48 + 15 + 5 + FONT_HGT_MEDIUM * 5, 0, 1, 0, 1,
            UIFont.NewSmall);
    end
    local modData = self.vehicle:getModData()
    local CrewLevel = modData.CrewLevelNow
    if not modData.CrewLevelNow then
        modData.CrewLevelNow = {}
        modData.CrewLevelNow["FastLoad"] = 0
        modData.CrewLevelNow["FastShootMG"] = 0
        modData.CrewLevelNow["Aim"] = 0
        modData.CrewLevelNow["Repair"] = 0
    end
    -- 技能点
    local TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, getText("IGUI_PanzerCrew_FastLoad"))
    self:drawText(getText("IGUI_PanzerCrew_FastLoad"), ((self.width - 300) / 2), 100 - FONT_HGT_SMALL, 1, 1, 1, 1,
        UIFont.NewSmall);
    self:drawText(tostring(modData.CrewLevelNow["FastLoad"]) .. "/10", ((self.width - 300) / 2) - 50, 100, 1, 1, 1, 1,
        UIFont.NewSmall);
    self:drawTextureScaled(nil, (self.width - 300) / 2, 100, 300, 25, 0.8, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, (self.width - 300) / 2, 100, 30 * modData.CrewLevelNow["FastLoad"], 25, 0.8, 0, 0.5, 0)

    TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, getText("IGUI_PanzerCrew_FastShootMG"))
    self:drawText(getText("IGUI_PanzerCrew_FastShootMG"), ((self.width - 300) / 2), 130, 1, 1, 1, 1, UIFont.NewSmall);
    self:drawText(tostring(modData.CrewLevelNow["FastShootMG"]) .. "/10", ((self.width - 300) / 2) - 50,
        130 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall);
    self:drawTextureScaled(nil, (self.width - 300) / 2, 130 + FONT_HGT_SMALL, 300, 25, 0.8, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, (self.width - 300) / 2, 130 + FONT_HGT_SMALL, 30 * modData.CrewLevelNow["FastShootMG"],
        25, 0.8, 0, 0.5, 0)
    TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, getText("IGUI_PanzerCrew_Aim"))
    self:drawText(getText("IGUI_PanzerCrew_Aim"), ((self.width - 300) / 2), 160 + FONT_HGT_SMALL, 1, 1, 1, 1,
        UIFont.NewSmall);
    self:drawText(tostring(modData.CrewLevelNow["Aim"]) .. "/10", ((self.width - 300) / 2) - 50,
        160 + FONT_HGT_SMALL * 2, 1, 1, 1, 1, UIFont.NewSmall);
    self:drawTextureScaled(nil, (self.width - 300) / 2, 160 + FONT_HGT_SMALL * 2, 300, 25, 0.8, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, (self.width - 300) / 2, 160 + FONT_HGT_SMALL * 2, 30 * modData.CrewLevelNow["Aim"], 25,
        0.8, 0, 0.5, 0)
    TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, getText("IGUI_PanzerCrew_BattleRepair"))
    self:drawText(getText("IGUI_PanzerCrew_BattleRepair"), ((self.width - 300) / 2), 190 + FONT_HGT_SMALL * 2, 1, 1, 1,
        1, UIFont.NewSmall);
    self:drawText(tostring(modData.CrewLevelNow["Repair"]) .. "/10", ((self.width - 300) / 2) - 50,
        190 + FONT_HGT_SMALL * 3, 1, 1, 1, 1, UIFont.NewSmall);
    self:drawTextureScaled(nil, (self.width - 300) / 2, 190 + FONT_HGT_SMALL * 3, 300, 25, 0.8, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, (self.width - 300) / 2, 190 + FONT_HGT_SMALL * 3, 30 * modData.CrewLevelNow["Repair"],
        25, 0.8, 0, 0.5, 0)
end

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

function ISPanzerCrewLevel:render()
    local vehicle = self.vehicle
    local Type = GetPanzerType(vehicle)

    local vehicleName = getText("IGUI_VehicleName" .. vehicle:getScript():getName())
    local TextHight = getTextManager():MeasureStringY(UIFont.NewSmall, vehicleName)
    self:drawTextureScaled(getTexture("media/textures/UI/" .. Type .. ".png"), 33, 15, 57, 48, 1, 1, 1, 1)

    local TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, vehicleName)
    self:drawText(vehicleName, 33 + (155 - TextLength - 5), 15 + 1, 1, 1, 1, 1, UIFont.NewSmall);

    self:drawText(getText("IGUI_PanzerCrew_Level"), self.width - 70, 30, 1, 1, 1, 1, UIFont.NewSmall);
    local modData = self.vehicle:getModData()
    local CrewLevel = modData.CrewLevel
    if not CrewLevel then
        CrewLevel = 0
    end

    self:drawText(tostring(CrewLevel) .. "/50", self.width - 70, 30 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall);
    self:drawText(getText("IGUI_PanzerCrew_FreePoint"), self.width - 70, 30 + FONT_HGT_SMALL * 2, 1, 1, 1, 1,
        UIFont.NewSmall);
    local CrewPoint = modData.CrewPoint
    if not CrewPoint then
        CrewPoint = 0
    end
    self:drawText(tostring(CrewPoint), self.width - 70, 30 + FONT_HGT_SMALL * 3, 1, 1, 1, 1, UIFont.NewSmall);
    -- 升级

end

function ISPanzerCrewLevel:refresh()
    self.close = ISButton:new(5, self.height - btnCloseHgt, btnCloseWid, btnCloseHgt, 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
    }
    self:addChild(self.close);

    self.TankImage = ISButton:new(33, 15, 155, 48, nil, self, nil)
    self.TankImage.internal = "TankImage";
    self.TankImage:initialise();
    self.TankImage:instantiate();
    self.TankImage.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    }
    self.TankImage.borderColor = {
        r = 0.8,
        g = 0.8,
        b = 0.8,
        a = 0.3
    }
    self:addChild(self.TankImage);
    local TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, "+")

    self.AddButtonFastLoad = ISButton:new((self.width + 300) / 2 + 5, 100, TextLength, FONT_HGT_SMALL, "+", self,
        this.onOptionMouseDown);
    self.AddButtonFastLoad.internal = "AddButtonFastLoad";
    self.AddButtonFastLoad:initialise();
    self.AddButtonFastLoad:instantiate();
    self.AddButtonFastLoad.tooltip = getText("IGUI_PanzerCrew_FastLoad_Desc")
    self.AddButtonFastLoad.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.2
    }

    self:addChild(self.AddButtonFastLoad);

    self.AddButtonFastShootMG = ISButton:new((self.width + 300) / 2 + 5, 130 + FONT_HGT_SMALL, TextLength,
        FONT_HGT_SMALL, "+", self, this.onOptionMouseDown);
    self.AddButtonFastShootMG.internal = "AddButtonFastShootMG";
    self.AddButtonFastShootMG:initialise();
    self.AddButtonFastShootMG:instantiate();
    self.AddButtonFastShootMG.tooltip = getText("IGUI_PanzerCrew_FastShootMG_Desc")
    self.AddButtonFastShootMG.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.2
    }
    self:addChild(self.AddButtonFastShootMG);

    self.AddButtonAim = ISButton:new((self.width + 300) / 2 + 5, 160 + FONT_HGT_SMALL * 2, TextLength, FONT_HGT_SMALL,
        "+", self, this.onOptionMouseDown);
    self.AddButtonAim.internal = "AddButtonAim";
    self.AddButtonAim:initialise();
    self.AddButtonAim:instantiate();
    self.AddButtonAim.tooltip = getText("IGUI_PanzerCrew_Aim_Desc")
    self.AddButtonAim.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.2
    }
    self:addChild(self.AddButtonAim);

    self.AddButtonRepair = ISButton:new((self.width + 300) / 2 + 5, 190 + FONT_HGT_SMALL * 3, TextLength,
        FONT_HGT_SMALL, "+", self, this.onOptionMouseDown);
    self.AddButtonRepair.internal = "AddButtonRepair";
    self.AddButtonRepair:initialise();
    self.AddButtonRepair:instantiate();
    self.AddButtonRepair.tooltip = getText("IGUI_PanzerCrew_BattleRepair_Desc")
    self.AddButtonRepair.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.2
    }
    self:addChild(self.AddButtonRepair);

    -- self.TankImage:setImage(getTexture("media/textures/UI/Tiger.png"));

end

function ISPanzerCrewLevel:create()
    self:refresh()
end

function ISPanzerCrewLevel:onOptionMouseDown(button, x, y)
    if button.internal == "Close" then
        self:destroy()
    end
    if button.internal == "AddButtonFastLoad" then
        local modData = self.vehicle:getModData()
        local CrewPoint = modData.CrewPoint
        if not CrewPoint then
            CrewPoint = 0
        end
        if CrewPoint > 0 then

            if not modData.CrewLevelNow then
                modData.CrewLevelNow = {}
                modData.CrewLevelNow["FastLoad"] = 0
                modData.CrewLevelNow["FastShootMG"] = 0
                modData.CrewLevelNow["Aim"] = 0
            end

            modData.CrewLevelNow["FastLoad"] = modData.CrewLevelNow["FastLoad"] + 1
            modData.CrewPoint = CrewPoint - 1
        end
    end
    if button.internal == "AddButtonFastShootMG" then
        local modData = self.vehicle:getModData()
        local CrewPoint = modData.CrewPoint
        if not CrewPoint then
            CrewPoint = 0
        end
        if CrewPoint > 0 then
            modData.CrewPoint = CrewPoint - 1
            if not modData.CrewLevelNow then
                modData.CrewLevelNow = {}
                modData.CrewLevelNow["FastLoad"] = 0
                modData.CrewLevelNow["FastShootMG"] = 0
                modData.CrewLevelNow["Aim"] = 0
            end
            modData.CrewLevelNow["FastShootMG"] = modData.CrewLevelNow["FastShootMG"] + 1
        end
    end
    if button.internal == "AddButtonAim" then
        local modData = self.vehicle:getModData()
        local CrewPoint = modData.CrewPoint
        if not CrewPoint then
            CrewPoint = 0
        end
        if CrewPoint > 0 then
            modData.CrewPoint = CrewPoint - 1
            if not modData.CrewLevelNow then
                modData.CrewLevelNow = {}
                modData.CrewLevelNow["FastLoad"] = 0
                modData.CrewLevelNow["FastShootMG"] = 0
                modData.CrewLevelNow["Aim"] = 0
            end
            modData.CrewLevelNow["Aim"] = modData.CrewLevelNow["Aim"] + 1
        end
    end
    if button.internal == "AddButtonRepair" then
        local modData = self.vehicle:getModData()
        local CrewPoint = modData.CrewPoint
        if not CrewPoint then
            CrewPoint = 0
        end
        if CrewPoint > 0 then
            modData.CrewPoint = CrewPoint - 1
            if not modData.CrewLevelNow then
                modData.CrewLevelNow = {}
                modData.CrewLevelNow["FastLoad"] = 0
                modData.CrewLevelNow["FastShootMG"] = 0
                modData.CrewLevelNow["Aim"] = 0
            end
            modData.CrewLevelNow["Repair"] = modData.CrewLevelNow["Repair"] + 1
        end
    end
end

function ISPanzerCrewLevel:update()
    ISPanel.update(self);
    local modData = self.vehicle:getModData()
    local CrewPoint = modData.CrewPoint
    local CrewLevel = modData.CrewLevelNow
    if not CrewPoint then
        CrewPoint = 0
    end
    if CrewPoint > 0 then
        if CrewLevel["FastLoad"] < 10 then
            self.AddButtonFastLoad:setVisible(true)
        else
            self.AddButtonFastLoad:setVisible(false)
        end
        if CrewLevel["FastShootMG"] < 10 then
            self.AddButtonFastShootMG:setVisible(true)
        else
            self.AddButtonFastShootMG:setVisible(false)
        end
        if CrewLevel["Aim"] < 10 then
            self.AddButtonAim:setVisible(true)
        else
            self.AddButtonAim:setVisible(false)
        end
        if CrewLevel["Repair"] < 10 then
            self.AddButtonRepair:setVisible(true)
        else
            self.AddButtonRepair:setVisible(false)
        end

    else
        self.AddButtonFastLoad:setVisible(false)
        self.AddButtonFastShootMG:setVisible(false)
        self.AddButtonAim:setVisible(false)
        self.AddButtonRepair:setVisible(false)
    end

end

function ISPanzerCrewLevel:new(x, y, width, height, player, vehicle)
    local o = {};
    o = ISPanel:new(x, y, width, height);
    setmetatable(o, self);
    o.character = player;
    self.__index = self;
    this.instance = o
    o.anchorRight = true
    o.hasPlot = false
    o.anchorBottom = true
    o.anchorTop = true
    o.anchorLeft = true
    o.moveWithMouse = true;
    o.NewItemPrice = 0;
    o.ShopLastIndex = 0;
    o.PlayerLastIndex = 0;
    o.NewItemCount = 0;
    o.vehicle = vehicle;
    o.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 1
    };
    o.borderColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    };
    return o;
end

