require "ISUI/ISPanel"
require('ISUI/ISScrollingListBox')
TurretAccessUnitUI = ISPanel:derive("PadUI");
local this = TurretAccessUnitUI
local FONT_HGT_SMALL = getTextManager():getFontHeight(UIFont.NewSmall)
local FONT_HGT_MEDIUM = getTextManager():getFontHeight(UIFont.NewMedium)
local FONT_HGT_LARGE = getTextManager():getFontHeight(UIFont.NewLarge)
local btnNewPlayer = getText("IGUI_WQ_NewPlayer")
local btnNewAdmin = getText("IGUI_WQ_NewAdmin")
local btnRemovePlayer = getText("IGUI_WQ_RemovePlayer")
local btnHgt = getTextManager():MeasureStringY(UIFont.NewSmall, btnNewAdmin) + 10
function TurretAccessUnitUI:initialise()
    ISPanel.initialise(self);
    self:create();
end
function TurretAccessUnitUI:prerender()
    ISPanel.prerender(self);
    if self.Point1 and self.Point2 then
        local x1, y1 = self.Point1:getX(), self.Point1:getY()
        local x2, y2 = self.Point2:getX(), self.Point2:getY()
        if x1 > x2 then
            local x = x1
            x1 = x2
            x2 = x
        end
        if y1 > y2 then
            local y = y1
            y1 = y2
            y2 = y
        end
        self.Point1 = getCell():getGridSquare(x1, y1, self.Point1:getZ())
        self.Point2 = getCell():getGridSquare(x2, y2, self.Point2:getZ())
    end
    local listitem = self.scrollingList.items[self.scrollingList.selected]
    if listitem then
        local index = listitem.index
        listitem = listitem.item
        if index == 7 then
            local tooltip = getText("IGUI_WQ_Grab_CorpseTip")
            self:drawText(tooltip, self.width / 2 + 20, self.height - FONT_HGT_LARGE * 3, 1, 1, 1, 1, UIFont.NewSmall)
        end
    end
end
function TurretAccessUnitUI:destroy()
    UIManager.setShowPausedMessage(true)
    self:setVisible(false)
    self:removeFromUIManager()
    this.instance = nil
end
function TurretAccessUnitUI:render()
end
function TurretAccessUnitUI:create()
    self.close = ISButton:new(self.width - 50, 0, 50, 50, nil, 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.close:setImage(getTexture("media/textures/close.png"));
    self:addChild(self.close);
    self.RemovePlayer = ISButton:new(self.width - 170, 20 + btnHgt, 100, btnHgt, btnRemovePlayer, self,
        this.onOptionMouseDown);
    self.RemovePlayer.internal = "RemovePlayer"
    self.RemovePlayer:initialise();
    self.RemovePlayer:instantiate();
    self.RemovePlayer.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.1
    }
    self:addChild(self.RemovePlayer);
    self.NewAdmin = ISButton:new(self.width - 170, (20 + btnHgt) * 2, 100, btnHgt, btnNewAdmin, self,
        this.onOptionMouseDown);
    self.NewAdmin.internal = "NewAdmin"
    self.NewAdmin:initialise();
    self.NewAdmin:instantiate();
    self.NewAdmin.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.1
    }
    self:addChild(self.NewAdmin);
    self.NewPlayer = ISButton:new(self.width - 170, (20 + btnHgt) * 3, 100, btnHgt, btnNewPlayer, self,
        this.onOptionMouseDown);
    self.NewPlayer.internal = "NewPlayer";
    self.NewPlayer:initialise();
    self.NewPlayer:instantiate();
    self.NewPlayer.borderColor = {
        r = 1,
        g = 1,
        b = 1,
        a = 0.1
    }
    self:addChild(self.NewPlayer);
    self.scrollingList = ISScrollingListBox:new(3, 20, self.width / 2, self.height - 25)
    self.scrollingList.anchorRight = true
    self.scrollingList.anchorBottom = true
    self.scrollingList.itemheight = FONT_HGT_SMALL * 2
    self.scrollingList:initialise();
    self.scrollingList:instantiate();
    self.scrollingList.selected = 0
    self.scrollingList.joypadParent = self;
    self.scrollingList.font = UIFont.NewMedium;
    self.scrollingList.doDrawItem = self.drawDatas
    self.scrollingList.drawBorder = true;
    self:addChild(self.scrollingList);
    self:initList()
end
function TurretAccessUnitUI:initList()
    local modData = self.turret:getModData()
    local UserTable = modData.User
    if UserTable and not UserTable[self.UserName] and not self.character:getAccessLevel() == "Admin" then

        return
    end
    if UserTable then
        for k, v in pairs(UserTable) do
            self.scrollingList:addItem(k, v)
        end
        print("check")
    else
        self.turret:getModData().User = {}
    end

end
function TurretAccessUnitUI:update()
    if self.turret and self.turret:getModData() then

    else
        self:destroy()
    end
    if self.turret:getModData() and self.turret:getModData().User and self.turret:getModData().User[self.UserName] then
        local MyAccess = self.turret:getModData().User[self.UserName].Access
        if MyAccess == "Owner" then
            self.NewAdmin:setVisible(true)
            self.NewPlayer:setVisible(true)
            self.RemovePlayer:setVisible(true)
        end
        if MyAccess == "Admin" then
            self.NewAdmin:setVisible(false)
            self.NewPlayer:setVisible(true)
            self.RemovePlayer:setVisible(true)
        end
        if MyAccess == "User" then
            self.NewAdmin:setVisible(false)
            self.NewPlayer:setVisible(false)
            self.RemovePlayer:setVisible(false)
        end
    else
        self.NewAdmin:setVisible(false)
        self.NewPlayer:setVisible(false)
        self.RemovePlayer:setVisible(false)
    end
    if not self.turret then
        self:destroy()
    end
    local listitem = self.scrollingList.items[self.scrollingList.selected]
    if listitem then
        listitem = listitem.item
        local UserName = listitem.User
        if self.UserName == UserName then
            self.NewAdmin:setVisible(false)
            self.NewPlayer:setVisible(false)
            self.RemovePlayer:setVisible(false)
        end
    else
        self.NewAdmin:setVisible(false)
        self.RemovePlayer:setVisible(false)
    end
    if self.character:getAccessLevel() == "Admin" then
        self.NewPlayer:setVisible(true)
        self.NewAdmin:setVisible(true)
        self.RemovePlayer:setVisible(true)
    end

end
function TurretAccessUnitUI:onOptionMouseDown(button, x, y)
    local listitem = self.scrollingList.items[self.scrollingList.selected]
    if listitem then
        listitem = listitem.item

        if button.internal == "RemovePlayer" then
            local UserName = listitem.User
            local modData = self.turret:getModData()
        end
        if button.internal == "NewAdmin" then
            local UserName = listitem.User
            local modData = self.turret:getModData()
            if modData.User[UserName].Access ~= "User" then
                local vehicle = getVehicleById(self.turret:getId())
                sendClientCommand("TurretSendAll", "SetAdmin", {
                    vehicleId = self.turret:getId(),
                    NewPlayerUser = UserName
                })
            end

        end
    end
    if button.internal == "NewPlayer" then
        local AddPlayerUI = TurretAccessAddPlayerUI:new(getCore():getScreenWidth() / 2 - 200,
            getCore():getScreenHeight() / 2 - 175, 400, 350, self.turret, self.player);
        AddPlayerUI:initialise()
        AddPlayerUI:addToUIManager()
        AddPlayerUI.safehouseUI = self;
        self.addPlayerUI = AddPlayerUI;
    end
    if button.internal == "Close" then
        self:destroy()
    end

end
function TurretAccessUnitUI:new(x, y, width, height, player, turret)
    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.turret = turret
    o.anchorLeft = true
    o.moveWithMouse = true;
    o.LastPoint1 = nil
    o.LastPoint2 = nil
    o.Point1 = nil
    o.Point2 = nil
    o.Point1Maker = nil
    o.Point2Maker = nil
    o.backgroundColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 1
    };
    o.borderColor = {
        r = 0,
        g = 0,
        b = 0,
        a = 0
    };
    o.UserName = o.character:getUsername()
    return o;
end
function TurretAccessUnitUI:drawDatas(_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)
    else
        self:drawRect(0, (_y), self:getWidth(), self.itemheight, 0.3, 1, 1, 1)
    end
    self:drawRectBorder(0, (_y), self:getWidth(), self.itemheight, 0.5, 1, 1, 1)
    if unit.User then
        self:drawText(unit.User, 10, (_y) + 2, 1, 1, 1, 1, UIFont.NewSmall)
    end
    if unit.Access then
        self:drawText(unit.Access, 70, (_y) + 2 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall)
        self:drawText(getText("IGUI_WQ_Access"), 10, (_y) + 2 + FONT_HGT_SMALL, 1, 1, 1, 1, UIFont.NewSmall)
    end

    return _y + self.itemheight
end
