class FormAlliancesSovereignty(Container):
    def CreateWindow(self):
        self.primeTimeInfo = None
        topCont = Container(parent=self,
                            height=24,
                            align=uiconst.TOTOP,
                            padding=const.defaultPadding)
        FillThemeColored(parent=topCont,
                         colorType=uiconst.COLORTYPE_UIHEADER,
                         opacity=0.15)
        EveLabelSmall(
            parent=topCont,
            text=GetByLabel('UI/Sovereignty/DefaultVulnerabilityTimeLabel'),
            align=uiconst.CENTERLEFT,
            left=const.defaultPadding)
        menuCont = Container(parent=topCont, align=uiconst.TORIGHT, width=18)
        self.primeTimeMenu = UtilMenu(parent=menuCont,
                                      align=uiconst.CENTERRIGHT,
                                      menuAlign=uiconst.TOPRIGHT,
                                      GetUtilMenu=self.PrimeTimeMenu)
        menuCont.display = False
        execCorpID = sm.GetService('alliance').GetAlliance(
            session.allianceid).executorCorpID
        timeTextLeft = const.defaultPadding
        if self._IsDirectorInExecCorp(execCorpID):
            timeTextLeft = 18
            menuCont.display = True
        self.timeLabel = EveLabelSmall(parent=topCont,
                                       align=uiconst.CENTERRIGHT,
                                       left=timeTextLeft)
        uthread.new(self.UpdatePrimeInfo)
        self.sovDashBoard = SovDashboard(parent=self)
        self.sovDashBoard.CreateWindow()

    def UpdatePrimeInfo(self):
        self.primeTimeInfo = sm.GetService('alliance').GetPrimeTimeInfo()
        self.UpdateText()

    def UpdateText(self):
        if self.primeTimeInfo is None or self.primeTimeInfo.currentPrimeHour is None:
            text = GetByLabel('UI/Common/Unknown')
        else:
            newPrimeHour = self.primeTimeInfo.newPrimeHour
            validAfter = self.primeTimeInfo.newPrimeHourValidAfter
            now = blue.os.GetWallclockTime()
            if newPrimeHour is not None and now < validAfter:
                text = GetByLabel(
                    'UI/Sovereignty/VulnerabilityTimeWithFutureChange',
                    hour=self.primeTimeInfo.currentPrimeHour,
                    newHour=self.primeTimeInfo.newPrimeHour,
                    validAfterDate=self.primeTimeInfo.newPrimeHourValidAfter)
            else:
                currentPrimeHour = self.GetCurrentPrimeHour()
                text = GetByLabel('UI/Sovereignty/VulnerabilityTime',
                                  hour=currentPrimeHour)
        self.timeLabel.SetText(text)

    def PrimeTimeMenu(self, menuParent):
        headerCont = menuParent.AddContainer(align=uiconst.TOTOP,
                                             height=20,
                                             padding=const.defaultPadding)
        EveLabelLargeBold(parent=headerCont,
                          text=GetByLabel('UI/Sovereignty/SetSovereigntyHour'),
                          align=uiconst.TOTOP)
        menuParent.AddSpace(height=10)
        text = menuParent.AddText(
            GetByLabel('UI/Sovereignty/SetNewVulnerabilityTimeDescription'))
        text.GetEntryWidth = lambda mc=text: 250
        cont = menuParent.AddContainer(align=uiconst.TOTOP,
                                       height=60,
                                       padding=const.defaultPadding)
        myCont = Container(name='myCont',
                           parent=cont,
                           align=uiconst.TOTOP,
                           height=22,
                           padTop=10)
        currentPrimeHour = self.GetCurrentPrimeHour()
        self.primeTimeCombo = Combo(name='primeTimeCombo',
                                    parent=myCont,
                                    options=self.GetTimeComboOptions(),
                                    select=currentPrimeHour,
                                    width=150)
        setBtn = Button(name='SetPrimeTimeBtn',
                        align=uiconst.TOPRIGHT,
                        parent=myCont,
                        label=GetByLabel('UI/Common/CommandSet'),
                        func=self.SetPrimeTime)

    def SetPrimeTime(self, *args):
        primeTimeNewValue = self.primeTimeCombo.GetValue()
        if self.primeTimeInfo and self.primeTimeInfo.newPrimeHour:
            newPrimeHour = self.primeTimeInfo.newPrimeHour
            validAfter = self.primeTimeInfo.newPrimeHourValidAfter
            newerPrimeHour = blue.os.GetWallclockTime(
            ) + CHANGE_PRIMETIME_DELAY
            if eve.Message(
                    'updateNewPrimeTime', {
                        'currentPendingTime': '%s:00' % newPrimeHour,
                        'currentPendingDate': FmtDate(validAfter, 'ss'),
                        'newPendingTime': '%s:00' % primeTimeNewValue,
                        'newPendingDate': FmtDate(newerPrimeHour, 'ss')
                    }, uiconst.YESNO) != uiconst.ID_YES:
                return
        sm.GetService('alliance').SetPrimeHour(primeTimeNewValue)
        self.UpdatePrimeInfo()
        self.primeTimeMenu.CloseMenu()

    def GetTimeComboOptions(self):
        return [(GetByLabel('UI/Sovereignty/VulnerabilityTime', hour=i), i)
                for i in xrange(24)]

    def GetCurrentPrimeHour(self):
        currentPrimeHour = 0
        if self.primeTimeInfo and self.primeTimeInfo.currentPrimeHour is not None:
            currentPrimeHour = self.primeTimeInfo.currentPrimeHour
        return currentPrimeHour

    def _IsDirectorInExecCorp(self, executorCorpID):
        if session.allianceid is None:
            return False
        if IsNPC(session.corpid):
            return False
        if session.corpid != executorCorpID:
            return False
        if corpRoleDirector & session.corprole != corpRoleDirector:
            return False
        return True
class AuctionEntry(BaseTeamEntry):
    default_name = 'AuctionEntry'

    def ApplyAttributes(self, attributes):
        BaseTeamEntry.ApplyAttributes(self, attributes)
        self.auctionCost = self.node.auctionCost
        self.AddColumnAuction()
        self.pathFinder = sm.GetService('clientPathfinderService')
        self.teamHandlerClient = sm.GetService('industryTeamSvc')
        self.minAmount = 0

    def AddColumnAuction(self):
        col = self.AddColumnContainer()
        col.OnMouseEnter = self.OnMouseEnter
        col.OnMouseExit = self.OnMouseExit
        col.OnClick = self.OnClick
        col.state = uiconst.UI_NORMAL
        col.GetMenu = self.GetMenu
        col.LoadTooltipPanel = self.LoadAuctionHintPanel
        bidCont = Container(parent=col, align=uiconst.TORIGHT, width=50)
        costCont = Container(parent=col, align=uiconst.TOALL, clipChildren=True)
        costText = FmtISK(self.auctionCost, 0)
        self.costLabel = EveLabelMedium(text=costText, parent=costCont, align=uiconst.CENTERLEFT, left=6)
        self.bidUtilMenu = UtilMenu(menuAlign=uiconst.TOPRIGHT, align=uiconst.CENTERLEFT, parent=bidCont, GetUtilMenu=self.BidOnTeamMenu, texturePath='res:/UI/Texture/Icons/73_16_50.png', left=2, label=GetByLabel('UI/Industry/Bid'))
        bidCont.width = self.bidUtilMenu.width + 10
        self.bidUtilMenu.hint = GetByLabel('UI/Industry/BidBtnHint')

    def BidOnTeamMenu(self, menuParent):
        cont = menuParent.AddContainer(align=uiconst.TOTOP, padding=const.defaultPadding)
        cont.GetEntryWidth = lambda mc = cont: 300
        topCont = Container(parent=cont, height=20, align=uiconst.TOTOP)
        self.searchCont = Container(parent=topCont)
        self.resultCont = Container(parent=topCont)
        self.resultCont.display = False
        self.systemLabel = EveLabelMediumBold(parent=self.resultCont, left=2, top=4)
        self.searchEdit = SinglelineEdit(parent=self.searchCont, align=uiconst.TOALL, hinttext=GetByLabel('UI/Industry/SearchForSystem'), width=0, top=0)
        self.searchEdit.OnReturn = lambda *args: self.SearchForLocation(self.searchEdit, *args)
        self.searchEdit.OnTab = lambda *args: self.SearchForLocation(self.searchEdit, *args)
        self.searchEdit.displayHistory = False
        ButtonIcon(parent=self.resultCont, align=uiconst.CENTERRIGHT, width=16, iconSize=16, texturePath='res:/UI/Texture/Icons/73_16_210.png', hint=GetByLabel('UI/Inventory/Clear'), func=self.ClearSystemSearch)
        self.resultScroll = Scroll(parent=cont, id='ResultScroll', align=uiconst.TOTOP, height=70)
        self.resultScroll.display = False
        Container(parent=cont, height=8, align=uiconst.TOTOP)
        self.bidHint = EveLabelMedium(parent=cont, align=uiconst.TOTOP, padding=(2, 4, 2, 4))
        bottomCont = Container(parent=cont, height=20, align=uiconst.TOTOP, padBottom=4)
        self.bidButton = Button(parent=bottomCont, label=GetByLabel('UI/Industry/Bid'), align=uiconst.TORIGHT, func=self.BidOnTeam)
        self.bidButton.OnMouseHover = self.BidHint
        self.bidButton.Disable()
        self.bidAmountEdit = SinglelineEdit(parent=bottomCont, align=uiconst.TOALL, width=0, floats=[0, 100000000000L, 0], padRight=4, top=0, hinttext='Enter amount')
        self.bidAmountEdit.OnReturn = self.BidOnTeam

    def BidHint(self):
        if self.bidButton.disabled:
            if self.searchEdit.GetValue():
                self.bidButton.hint = GetByLabel('UI/Industry/BidBtnSearchHint')
        else:
            self.bidButton.hint = ''

    def BidOnTeam(self, *args):
        if not self.searchedSystem:
            return
        if self.IsUnreachable():
            self.bidAmountEdit.SetValue('')
            self.bidHint.text = GetByLabel('UI/Industry/UnreachableSystem')
            return
        amount = self.bidAmountEdit.GetValue()
        if not self.HasSolarSystemBidForAuctionID() and amount <= self.minAmount:
            self.bidAmountEdit.SetValue('')
            self.bidHint.text = GetByLabel('UI/Industry/TeamBidTooLow', minAmount=FmtAmt(self.minAmount, showFraction=0))
            return
        self.teamHandlerClient.BidOnTeam(self.team.teamID, self.searchedSystem, amount)
        self.costLabel.text = self.GetForcedAuctionCost()
        self.bidUtilMenu.CloseMenu()

    def SearchForLocation(self, edit, *args):
        searchText = edit.GetValue()
        if not searchText or searchText == '':
            return
        groupIDList = [const.searchResultSolarSystem, const.searchResultWormHoles]
        searchResult = QuickSearch(searchText.strip(), groupIDList)
        noOfResults = len(searchResult)
        if noOfResults == 1:
            self.ConfirmSystem(searchResult[0])
        elif noOfResults:
            self.resultScroll.display = True
            scrollList = self.GetScrollList(searchResult)
            self.resultScroll.LoadContent(contentList=scrollList)
        else:
            edit.SetHintText(GetByLabel('UI/Station/BountyOffice/NoOneFound'))

    def LoadAuctionHintPanel(self, tooltipPanel, *args):
        tooltipPanel.LoadGeneric3ColumnTemplate()
        tooltipPanel.AddLabelLarge(text=GetByLabel('UI/Industry/SystemBidList'), colSpan=tooltipPanel.columns, align=uiconst.CENTER)
        tooltipPanel.AddSpacer(colSpan=tooltipPanel.columns, width=0, height=2)
        topSystems = self.GetTopSystems()
        if topSystems:
            for i, (amount, solarSystemID) in enumerate(topSystems):
                if i > 2:
                    break
                systemName = self.FormatSystemName(solarSystemID)
                systemLabel = '%i %s' % (i + 1, systemName)
                tooltipPanel.AddLabelSmall(text=systemLabel, colSpan=1)
                tooltipPanel.AddSpacer(width=10, height=0)
                tooltipPanel.AddLabelSmall(text=FmtISK(amount, 0), colSpan=1, align=uiconst.TORIGHT)

        myBids = self.GetMyBids()
        if myBids:
            tooltipPanel.AddSpacer(colSpan=tooltipPanel.columns, width=0, height=6)
            tooltipPanel.AddLabelLarge(text=GetByLabel('UI/Industry/MyBidList'), colSpan=tooltipPanel.columns, align=uiconst.CENTER)
            tooltipPanel.AddSpacer(colSpan=tooltipPanel.columns, width=0, height=2)
            for solarSystemID, amount in myBids:
                systemName = self.FormatSystemName(solarSystemID)
                tooltipPanel.AddLabelSmall(text=systemName, colSpan=1)
                tooltipPanel.AddSpacer(width=10, height=0)
                tooltipPanel.AddLabelSmall(text=FmtISK(amount, 0), colSpan=1, align=uiconst.TORIGHT)

    def GetTopSystems(self):
        bids = self.GetBids()
        return bids.GetRankedBids()

    def GetMyBids(self):
        bids = self.GetBids()
        myBids = GetMyBidsBySolarSystem(session.charid, bids)
        if myBids:
            return sorted(myBids.iteritems(), key=itemgetter(1), reverse=True)

    def GetScrollList(self, results):
        scrollList = []
        for solarSystemID in results:
            data = util.KeyVal()
            data.label = self.FormatSystemName(solarSystemID)
            data.OnDblClick = self.DblClickEntry
            data.solarSystemID = solarSystemID
            entry = listentry.Get('Generic', data)
            scrollList.append(entry)

        return scrollList

    def FormatSystemName(self, solarSystemID):
        systemName = cfg.evelocations.Get(solarSystemID).name
        secStatus = self.GetSecurityLabel(solarSystemID)
        return '%s %s' % (systemName, secStatus)

    def DblClickEntry(self, entry, *args):
        solarSystemID = entry.sr.node.solarSystemID
        if solarSystemID:
            self.ConfirmSystem(solarSystemID)

    def ConfirmSystem(self, solarSystemID):
        self.resultScroll.display = False
        self.searchCont.display = False
        self.resultCont.display = True
        self.searchEdit.SetValue('')
        self.searchedSystem = solarSystemID
        self.systemLabel.text = self.FormatSystemName(solarSystemID)
        if self.IsUnreachable():
            self.bidHint.text = GetByLabel('UI/Industry/UnreachableSystem')
        elif not self.HasSolarSystemBidForAuctionID():
            self.minAmount = GetDistanceCost(self.pathFinder, self.team.solarSystemID, solarSystemID)
            self.bidHint.text = GetByLabel('UI/Industry/TeamMinBid', minAmount=FmtAmt(self.minAmount, showFraction=0))
        self.bidButton.Enable()
        uicore.registry.SetFocus(self.bidAmountEdit)

    def ClearSystemSearch(self, *args):
        self.searchedSystem = None
        self.resultCont.display = False
        self.systemLabel.text = ''
        self.bidHint.text = ''
        self.bidHint.height = 0
        self.searchCont.display = True
        self.bidButton.Disable()

    def GetForcedAuctionCost(self):
        return FmtISK(GetHighestBid(self.GetBids()), 0)

    def HasSolarSystemBidForAuctionID(self):
        return self.teamHandlerClient.HasSolarSystemBidForAuctionID(self.team.teamID, self.searchedSystem)

    def IsUnreachable(self):
        return bool(not util.IsWormholeSystem(self.searchedSystem) and self.pathFinder.GetJumpCount(self.team.solarSystemID, self.searchedSystem) > 1000)

    @staticmethod
    def GetDefaultColumnWidth():
        return {GetByLabel('UI/Industry/Team'): 160,
         GetByLabel('UI/Common/LocationTypes/System'): 80,
         GetByLabel('UI/Common/Jumps'): 50,
         GetByLabel('UI/Common/Security'): 55,
         GetByLabel('UI/Industry/Bonuses'): 310,
         GetByLabel('UI/Industry/Salary'): 50,
         GetByLabel('UI/Industry/AuctionEnds'): 110,
         GetByLabel('UI/Industry/Auction'): 110}

    @staticmethod
    def GetHeaders():
        return (GetByLabel('UI/Industry/Team'),
         GetByLabel('UI/Common/LocationTypes/System'),
         GetByLabel('UI/Common/Jumps'),
         GetByLabel('UI/Common/Security'),
         GetByLabel('UI/Industry/Activity'),
         GetByLabel('UI/Industry/Bonuses'),
         GetByLabel('UI/Industry/Salary'),
         GetByLabel('UI/Industry/AuctionEnds'),
         GetByLabel('UI/Industry/Auction'))

    @staticmethod
    def GetColumnSortValues(teamData, teamName, bonusSum, expiryTime, teamCost, auctionCost):
        return (teamName,
         cfg.evelocations.Get(teamData.team.solarSystemID).name,
         sm.GetService('clientPathfinderService').GetJumpCountFromCurrent(teamData.team.solarSystemID),
         sm.GetService('map').GetSecurityStatus(teamData.team.solarSystemID),
         teamData.team.activity,
         bonusSum,
         teamCost,
         expiryTime,
         auctionCost)

    def GetBids(self):
        return self.teamHandlerClient.GetBids(self.team.teamID)

    def GetQAMenu(self):
        return [['QA: Expire Auction', self.GMExpireAuction, [self.team.teamID]], ['QA: Print Bids', self.ShowBids, []], ['QA: Set Expiry time', self.SetExpiryTime]]

    def SetExpiryTime(self):
        ret = uiutil.NamePopup(caption='Set Expiry Time', label='Type in Expiry Time', setvalue=util.FmtDate(self.node.expiryTime))
        if ret is None:
            return
        newTime = util.ParseDateTime(ret)
        self.teamHandlerClient.GMSetAuctionExpiryTime(self.team.teamID, newTime)