Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Win.Templates.ActionContainers
Imports DevExpress.ExpressApp.Model
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.ExpressApp.SystemModule
Imports DevExpress.XtraTreeList
Imports System.Drawing
Imports DevExpress.Data.Filtering

Partial Public Class NavbarTreeList
    Inherits WindowController
    Private navActionContainer As NavigationActionContainer
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        _ObjectSpace = Application.CreateObjectSpace()
        _ListViewModel = CType(Application.FindModelView("RegistryTasks_ListView_Not_Completed"), IModelListView)
        AddHandler Window.ProcessActionContainer, AddressOf Window_ProcessActionContainer
    End Sub
    Protected Overrides Sub OnDeactivated()
        RemoveHandler Window.ProcessActionContainer, AddressOf Window_ProcessActionContainer
        UnsubscribeFromContainerEvents()
        navActionContainer = Nothing
        MyBase.OnDeactivated()
    End Sub
    Private Sub Window_ProcessActionContainer(ByVal sender As Object, ByVal e As ProcessActionContainerEventArgs)
        UnsubscribeFromContainerEvents()
        If TypeOf e.ActionContainer Is NavigationActionContainer Then
            navActionContainer = TryCast(e.ActionContainer, NavigationActionContainer)
            SubscribeToContainerEvents()
            SetupNavBar()
        End If
    End Sub
    Private Sub SubscribeToContainerEvents()
        If navActionContainer IsNot Nothing Then
            AddHandler navActionContainer.NavigationControlCreated, AddressOf navigationControlCreated
        End If
    End Sub
    Private Sub UnsubscribeFromContainerEvents()
        If navActionContainer IsNot Nothing Then
            RemoveHandler navActionContainer.NavigationControlCreated, AddressOf navigationControlCreated
        End If
    End Sub
    Private Sub navigationControlCreated(ByVal sender As Object, ByVal e As EventArgs)
        SetupNavBar()
    End Sub
    Private _TreeNode As TreeListNode
    Private _ObjectSpace As Xpo.XPObjectSpace
    Private _ListViewModel As IModelListView
    Private Sub SetupNavBar()
        If navActionContainer IsNot Nothing AndAlso TypeOf navActionContainer.NavigationControl Is TreeListNavigationControl Then
            Dim treeListNavigationControl As TreeListNavigationControl = CType(navActionContainer.NavigationControl, TreeListNavigationControl)
            Dim navItem As ChoiceActionItem = Window.GetController(Of ShowNavigationItemController)().ShowNavigationItemAction.Items.Find("@RegistryTask_ListView_Not_Completed", ChoiceActionItemFindType.Recursive, ChoiceActionItemFindTarget.Any)
            If navItem IsNot Nothing Then
                _TreeNode = treeListNavigationControl.FindNodeByActionItem(navItem)
                AddHandler treeListNavigationControl.AfterExpand, AddressOf NavigationTreeList_AfterExpand
                AddHandler treeListNavigationControl.NodeCellStyle, AddressOf NavigationTreeList_NodeCellStyle
            End If
        End If
    End Sub
    Private Sub NavigationTreeList_NodeCellStyle(ByVal sender As Object, ByVal e As GetCustomNodeCellStyleEventArgs)
        If e.Node.GetDisplayText(e.Node.TreeList.Columns(0)).Contains(" [") Then
            e.Appearance.Font = New Font(e.Appearance.Font, FontStyle.Bold)
        End If
    End Sub
    Private Sub NavigationTreeList_AfterExpand(ByVal sender As Object, ByVal e As NodeEventArgs)
        For Each node As TreeListNode In e.Node.Nodes
            If node.Equals(_TreeNode) Then
                Dim caption As String = _TreeNode.GetValue(_TreeNode.TreeList.Columns(0))
                If caption.Contains(" [") Then
                    caption = caption.Remove(caption.IndexOf(" ["))
                End If
                Dim count As Integer = CInt(_ObjectSpace.Session.Evaluate(_ListViewModel.ModelClass.TypeInfo.Type, New AggregateOperand("", Aggregate.Count), CriteriaOperator.Parse("Status<>4 and HRPerson.User.UserName=?", SecuritySystem.CurrentUserName)))
                If count > 0 Then
                    caption &= String.Format(" [{0}]", count)
                End If
                node.SetValue(_TreeNode.TreeList.Columns(0), caption)
            End If
        Next
    End Sub


End Class
