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

local btnClose = getText("IGUI_CraftUI_Close")
local btnUpload = getText("IGUI_ISHeliUpdate_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 ISHeliUpdate:initialise()
    ISPanel.initialise(self);
    self:create();
end

function ISHeliUpdate:prerender()
    ISPanel.prerender(self);
    local Type = GetHeliType(self.vehicle)
    self:drawTextureScaled(nil, -512, 0, 512, self.height, 1, 0, 0, 0)
    self:drawTextureScaled(getTexture("media/textures/UI/background/BackGroundAll.png"), -512, 0, self.width + 512,
        self.height, 1, 1, 1, 1)
    self:drawTextureScaled(getTexture("media/textures/UI/background/" .. HeliList[Type].CrewLaungage .. ".png"), -512,
        0, 256, 128, 1, 1, 1, 1)
    self:drawTextureScaled(getTexture("media/textures/UI/background/" .. Type .. ".png"), -512, 0, 512, 256, 1, 1, 1, 1)

    self:drawTextureScaled(getTexture("media/textures/UI/Detail.png"), -512, 256 + 10, 357, 357, 1, 1, 1, 1)
    self:drawText(getText("IGUI_Vehicle_Detail_Rank"), -512 + 25, 360 + 40, 0.5, 0.5, 0.5, 0.5, UIFont.NewSmall)
    self:drawText(HeliList[Type].Rank, -512 + 25, 360, 1, 1, 1, 1, UIFont.NewLarge)

    for i = 1, #HeliList[Type].BattleRating do
        if i % 2 == 0 then
            self:drawText(HeliList[Type].BattleRating[i], -512 + 150 + (i - 2) * 30, 360 + 20, 1, 1, 1, 1,
                UIFont.NewSmall)
        else
            self:drawText(getText("IGUI_Vehicle_Detail_BattleRating_" .. HeliList[Type].BattleRating[i]),
                -512 + 150 + (i - 1) * 30, 360, 0.5, 0.5, 0.5, 1, UIFont.NewSmall)
        end
    end

    self:drawText(getText("IGUI_Vehicle_Detail_BattleRating"), -512 + 150, 360 + 40, 0.5, 0.5, 0.5, 0.5, UIFont.NewSmall)

    self:drawText(getText("IGUI_Vehicle_Detail_Faction"), -512 + 25, 360 + 40 + 52, 0.5, 0.5, 0.5, 0.5, UIFont.NewSmall)
    self:drawTextureScaled(getTexture("media/textures/UI/" .. HeliList[Type].Faction .. ".png"), -512 + 25, 360 + 75,
        25, 17.5, 1, 1, 1, 1)
    self:drawText(getText("IGUI_Vehicle_Detail_MainRole"), -512 + 150 + 10, 360 + 40 + 52, 0.5, 0.5, 0.5, 0.5,
        UIFont.NewSmall)
    self:drawText(getText("IGUI_Vehicle_Detail_" .. HeliList[Type].MainRole), -512 + 150 + 10, 360 + 65, 1, 1, 1, 1,
        UIFont.NewMedium)

    self:drawText(getText("IGUI_Vehicle_Detail_Purchase"), -512 + 150 + 35, 360 + 40 + 106, 0.5, 0.5, 0.5, 0.5,
        UIFont.NewSmall)
    self:drawText(HeliList[Type].Purchase, -512 + 150 + 35, 360 + 106 + 15, 1, 1, 1, 1, UIFont.NewMedium)
    self:drawText(getText("IGUI_Vehicle_Detail_Research"), -512 + 25, 360 + 40 + 106, 0.5, 0.5, 0.5, 0.5,
        UIFont.NewSmall)
    self:drawText(HeliList[Type].Research, -512 + 25, 360 + 106 + 15, 1, 1, 1, 1, UIFont.NewMedium)

    self:drawTextureScaled(getTexture("media/textures/UI/BackGround.png"), 33, 15, 155, 48, 1, 1, 1, 1)

    self:drawTextureScaled(getTexture("media/textures/UI/BackGround.png"), 33 + 155 + 10, 125, 155, 48, 1, 1, 1, 1)

    self:drawTextureScaled(getTexture("media/textures/UI/BackGround.png"), 33 + 155 * 3 + 10 * 3, 125, 155, 48, 1, 1, 1,
        1)
    self:drawTextureScaled(getTexture("media/textures/UI/BackGround.png"), 33 + 155 * 2 + 10 * 2, 125, 155, 48, 1, 1, 1,
        1)
    self:drawTextureScaled(getTexture("media/textures/UI/" .. Type .. ".png"), 33, 15, 57, 48, 1, 1, 1, 1)

    self:drawTextureScaled(nil, 33, 75, 155, 38, 1, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, 33 + 10 + 155, 75, 155, 38, 1, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, 33 + 10 * 2 + 155 * 2, 75, 155, 38, 1, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, 33 + 10 * 3 + 155 * 3, 75, 155, 38, 1, 0.2, 0.2, 0.2)

    self:drawTextureScaled(nil, 33, 185, 155 + 10 + 155, 38, 1, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, 33 + 155 * 2 + 10 * 2, 185, 155, 38, 1, 0.2, 0.2, 0.2)
    self:drawTextureScaled(nil, 33 + 155 * 3 + 10 * 3, 185, 155, 38, 1, 0.2, 0.2, 0.2)

    self:drawTextureScaled(getTexture("media/textures/UI/ResearchPoint.png"), self.width - 90, self.height - 50, 20, 20,
        1, 1, 1, 1)

    -- 升级
    -- self:drawTextureScaled(getTexture("media/textures/UI/BackGround.png"), 33 + 155 * 3 + 10 * 3, 235, 155, 48, 1, 1, 1,
    -- 1)

    local TigerUpdateList = HeliList[Type].UpdateList
    if not TigerUpdateList then
        return
    end
    for k, v in pairs(TigerUpdateList) do
        local row = v.Postion.row
        local col = v.Postion.col
        local x = 33 + 155 * (col - 1) + 10 * (col - 1)
        local y = 235 + 48 * (row - 1) + 10 * (row - 1)
        self:drawTextureScaled(getTexture("media/textures/UI/BackGround.png"), x, y, 155, 48, 1, 1, 1, 1)
    end
end

function ISHeliUpdate:destroy()
    local playerObj = getPlayer()
    local vehicle = playerObj:getVehicle()
    if getWorld():getGameMode() == "Multiplayer" then
        if vehicle and vehicle:getSeat(playerObj) == 0 then
            local modData = vehicle:getModData()
            local CrewLevelNow = modData.CrewLevelNow
            local CrewPoint = modData.CrewPoint
            local CrewLevel = modData.CrewLevel
            local CrewKillNum = modData.CrewKillNum
            local researchTable = modData.researchTable
            local researchPoint = modData.researchPoint
            local OnlinePlayers = getOnlinePlayers()
            local SendPlayerTable = {}
            for i = 1, OnlinePlayers:size() do
                local player = OnlinePlayers:get(i - 1)
                table.insert(SendPlayerTable, player:getUsername())
            end
            sendClientCommand(playerObj, "HeliSend", "Update", {
                vehicleId = vehicle:getId(),
                CrewLevelNow = CrewLevelNow,
                CrewPoint = CrewPoint,
                CrewLevel = CrewLevel,
                researchTable = researchTable,
                researchPoint = researchPoint,
                CrewKillNum = CrewKillNum,
                SendPlayerTable = SendPlayerTable
            })
        end
    end
    UIManager.setShowPausedMessage(true)
    self:setVisible(false)
    self:removeFromUIManager()
end

function ISHeliUpdate:render()
    local Type = GetHeliType(self.vehicle)
    local vehicleName = getText("IGUI_VehicleName" .. self.vehicle:getScript():getName())
    local TextHight = getTextManager():MeasureStringY(UIFont.NewSmall, vehicleName)
    -- self:drawTextureScaled(getTexture("media/textures/UI/HE.png"), 33 + 155 * 3 + 10 * 3, 235, 48, 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);

    local Weaponlist = HeliList[Type].UpdateUIList
    self:drawText(getText("IGUI_MainGun"), 33 + 5, 75 + TextHight / 2, 1, 1, 1, 1, UIFont.NewSmall);
    if Weaponlist then
        for i, v in ipairs(Weaponlist) do
            self:drawTextureScaled(getTexture("media/textures/UI/" .. v[2] .. ".png"), 33 + 155 * (i) + 10 * (i) + 5,
                125, 48, 48, 1, 1, 1, 1)
            local TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, v[1])
            self:drawText(v[1], 33 + 155 * (i) + 10 * (i) + 5, 75 + TextHight / 2, 1, 1, 1, 1, UIFont.NewSmall);
            self:drawText(v[1], 33 + 155 * (i) + 10 * (i) + 150 - TextLength, 125 + TextHight / 2, 1, 1, 1, 1,
                UIFont.NewSmall)
        end
    end

    self:drawText(getText("IGUI_VehicleUpdate"), 33 + 5, 185 + TextHight / 2, 1, 1, 1, 1, UIFont.NewSmall);
    self:drawText(getText("IGUI_VehicleDefUpdate"), 33 + 155 * 2 + 10 * 2 + 5, 185 + TextHight / 2, 1, 1, 1, 1,
        UIFont.NewSmall);
    self:drawText(getText("IGUI_VehicleAtkUpdate"), 33 + 155 * 3 + 10 * 3 + 5, 185 + TextHight / 2, 1, 1, 1, 1,
        UIFont.NewSmall);

    self:drawText(getText("IGUI_ResearchPoint"), self.width - 70, self.height - 50, 1, 1, 1, 1, UIFont.NewSmall);
    local modData = self.vehicle:getModData()
    local researchPoint = modData.researchPoint
    if not researchPoint then
        researchPoint = 0
    end

    self:drawText(tostring(researchPoint), self.width - 70, self.height - 30, 1, 1, 1, 1, UIFont.NewSmall);
    local Type = GetHeliType(self.vehicle)
    local TigerUpdateList = HeliList[Type].UpdateList
    if not TigerUpdateList then
        return
    end
    -- 升级
    if not TigerUpdateList then
        return
    end
    for k, v in pairs(TigerUpdateList) do
        local row = v.Postion.row
        local col = v.Postion.col
        local x = 33 + 155 * (col - 1) + 10 * (col - 1)
        local y = 235 + 48 * (row - 1) + 10 * (row - 1)
        self:drawTextureScaled(v.Texture, x, y, 48, 48, 1, 1, 1, 1)
        local TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, v.Name)
        self:drawText(v.Name, x + (155 - TextLength - 5), y + 40 / 2 - FONT_HGT_SMALL / 2, 1, 1, 1, 1, UIFont.NewSmall);
        TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, tostring(v.Point))

        local modData = self.vehicle:getModData()
        if not (modData.researchTable and modData.researchTable[k]) then
            self:drawTextureScaled(nil, x, y, 155, 48, 0.3, 0.8, 0.2, 0.2)
            self:drawText(tostring(v.Point), x + (155 - TextLength - 5),
                y + 40 / 2 - FONT_HGT_SMALL / 2 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall);
            self:drawTextureScaled(getTexture("media/textures/UI/ResearchPoint.png"), x + (155 - TextLength - 5) - 18,
                y + 40 / 2 - FONT_HGT_SMALL / 2 + FONT_HGT_SMALL, 18, 18, 1, 1, 1, 1)
        else
            TextLength = getTextManager():MeasureStringX(UIFont.NewSmall, getText("IGUI_Researched"))
            self:drawText(getText("IGUI_Researched"), x + (155 - TextLength - 5),
                y + 40 / 2 - FONT_HGT_SMALL / 2 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall);
        end
    end
end

function ISHeliUpdate: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.1
    }
    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
    }

    local Type = GetHeliType(self.vehicle)

    self.TankImage:setTooltip(getText("IGUI_VehicleName" .. Type .. "_Tooltip"))
    self:addChild(self.TankImage);

    self.MainGun = ISButton:new(33 + 155 + 10, 125, 155, 48, nil, self, nil)
    self.MainGun.internal = "MainGun";
    self.MainGun:initialise();
    self.MainGun:instantiate();
    self.MainGun.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    }
    self.MainGun:setTooltip(HeliList[Type].MainGunTooltip)
    self.MainGun.borderColor = {
        r = 0.8,
        g = 0.8,
        b = 0.8,
        a = 0.3
    }
    self:addChild(self.MainGun);

    -- 升级
    local TigerUpdateList = HeliList[Type].UpdateList
    if not TigerUpdateList then
        return
    end
    for k, v in pairs(TigerUpdateList) do
        local row = v.Postion.row
        local col = v.Postion.col
        local x = 33 + 155 * (col - 1) + 10 * (col - 1)
        local y = 235 + 48 * (row - 1) + 10 * (row - 1)
        self[k] = ISButton:new(x, y, 155, 48, nil, self, this.onOptionMouseDown)
        self[k].internal = k;
        self[k]:initialise();
        self[k]:instantiate();
        self[k].backgroundColor = {
            r = 0,
            g = 0,
            b = 0,
            a = 0
        }
        self[k].borderColor = {
            r = 0.8,
            g = 0.8,
            b = 0.8,
            a = 0.3
        }

        if v.Tooltip then
            self[k]:setTooltip(v.Tooltip)
        end
        self:addChild(self[k]);
    end

    self.ResearchButton = ISButton:new(5 + btnCloseWid + 5, self.height - btnCloseHgt, btnCloseWid, btnCloseHgt,
        getText("IGUI_Research"), self, this.onOptionMouseDown);
    self.ResearchButton.internal = "ResearchButton";
    self.ResearchButton:initialise();
    self.ResearchButton:instantiate();
    self.ResearchButton.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.1
    }
    self:addChild(self.ResearchButton);
end

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

function ISHeliUpdate:onOptionMouseDown(button, x, y)
    local Type = GetHeliType(self.vehicle)
    local TigerUpdateList = HeliList[Type].UpdateList
    if button.internal == "Close" then
        self:destroy()
    end
    if button.internal == "ResearchButton" then
        if not TigerUpdateList then
            return
        end
        if not self.SelectNow then
            return
        end
        local SelectTable = TigerUpdateList[self.SelectNow]
        local modData = self.vehicle:getModData()
        local researchPoint = modData.researchPoint
        if not researchPoint then
            researchPoint = 0
        end
        if researchPoint < SelectTable.Point then
            return
        end
        if not modData.researchTable then
            modData.researchTable = {}
        end
        if modData.researchTable[self.SelectNow] and not TigerUpdateList[self.SelectNow].ReResearch then
            return
        end
        modData.researchTable[self.SelectNow] = true
        modData.researchPoint = researchPoint - SelectTable.Point
        if self.SelectNow == "ArtSupport" then
            if not modData.ArtSupportTimes then
                modData.ArtSupportTimes = 0
            end
            modData.ArtSupportTimes = modData.ArtSupportTimes + 1
        end
        if self.SelectNow == "AnTiPersonnel" then
            if not modData.AnTiPersonnelTimes then
                modData.AnTiPersonnelTimes = 0
            end
            modData.AnTiPersonnelTimes = modData.AnTiPersonnelTimes + 1
        end
        if self.SelectNow == "AirStrike" then
            if not modData.AirStrikeTimes then
                modData.AirStrikeTimes = 0
            end
            modData.AirStrikeTimes = modData.AirStrikeTimes + 1
        end
        self.vehicle:transmitModData()
    end
    local type = button.internal
    if not TigerUpdateList then
        return
    end
    if TigerUpdateList[type] then
        self.SelectNow = type
        self[type].backgroundColor = {
            r = 0.3,
            g = 0.8,
            b = 0.2,
            a = 0.2
        }
    end
end

function ISHeliUpdate:update()
    local Type = GetHeliType(self.vehicle)
    local TigerUpdateList = HeliList[Type].UpdateList
    if not TigerUpdateList then
        return
    end
    if SandboxVars.WT.UnlockAllRearch then
        local modData = self.vehicle:getModData()
        if not modData.researchTable then
            modData.researchTable = {}
        end
        for k, v in pairs(TigerUpdateList) do
            if not modData.researchTable[k] then
                modData.researchTable[k] = true
            end
            if v.ReResearch then
                modData.researchTable[k] = true
                if k == "ArtSupport" then
                    if not modData.ArtSupportTimes then
                        modData.ArtSupportTimes = 100
                    end
                end
                if k == "AnTiPersonnel" then
                    if not modData.AnTiPersonnelTimes then
                        modData.AnTiPersonnelTimes = 100
                    end
                end
                if k == "AirStrike" then
                    if not modData.AirStrikeTimes then
                        modData.AirStrikeTimes = 100
                    end
                end
            end
        end
    end
    for k, v in pairs(TigerUpdateList) do
        if self[k] then
            if self.SelectNow then
                if self.SelectNow == k then
                    self[k].backgroundColor = {
                        r = 0.3,
                        g = 0.8,
                        b = 0.2,
                        a = 0.2
                    }
                else
                    self[k].backgroundColor = {
                        r = 0,
                        g = 0,
                        b = 0,
                        a = 0
                    }
                end
            else
                self[k].backgroundColor = {
                    r = 0,
                    g = 0,
                    b = 0,
                    a = 0
                }
            end
        end
    end
    if not self.SelectNow then
        return
    end
    local SelectTable = TigerUpdateList[self.SelectNow]
    local modData = self.vehicle:getModData()
    local colNow = SelectTable.Postion.col
    local rowNow = SelectTable.Postion.row
    if rowNow > 1 then
        local rowBefore = rowNow - 1
        local typeBefore = nil
        for k, v in pairs(TigerUpdateList) do
            if v.Postion.col == colNow and v.Postion.row == rowBefore then
                typeBefore = k
            end
        end
        if typeBefore then
            if not modData.researchTable then
                modData.researchTable = {}
            end
            if modData.researchTable[typeBefore] then
                self.ResearchButton:setEnable(true)
                self.ResearchButton:setTooltip(nil)
            else
                self.ResearchButton:setEnable(false)
                self.ResearchButton:setTooltip(getText("IGUI_ResearchBefore"))
                return
            end
        end
    else
        self.ResearchButton:setEnable(true)
    end
    if modData.researchTable and modData.researchTable[self.SelectNow] and
        not TigerUpdateList[self.SelectNow].ReResearch then
        self.ResearchButton:setEnable(false)
        self.ResearchButton:setTooltip(getText("IGUI_Researched"))
        return
    else
        self.ResearchButton:setTooltip(nil)
    end
    local PointNow = modData.researchPoint
    if not PointNow then
        PointNow = 0
    end
    if PointNow < SelectTable.Point then
        self.ResearchButton:setEnable(false)
    else
        self.ResearchButton:setEnable(true)
    end

end

function ISHeliUpdate: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

