Imports System.Text
Imports System.Xml

Namespace UTSModule.Station

    Public Class StationTestPlan
        Inherits StationPlan

        Sub New(packet As StationPacket)
            MyBase.New(packet)

            TestPlanLocked = False
            ParentPacket = packet

            HeadNode = New RowNode()
            HeadNode.HeadNode = HeadNode
            HeadNode.RowType = RowNode.RowTypeEnum.FixedModule
            HeadNode.RowLever = -1
            HeadNode.RowIndex = 0
            HeadNode.StationPlan = Me
            HeadNode.Label = $"StationPlan"
            HeadNode.Description = $"StationPlan"

            CreateStationPlan()
        End Sub

        ''' <summary>测试流程是否已锁定</summary>
        Public Property TestPlanLocked As Boolean

        ''' <summary>
        ''' 头节点
        ''' </summary>
        ''' <returns></returns>
        Friend Property HeadNode As RowNode


        ''' <summary>
        ''' 节点树状集合
        ''' </summary>
        ''' <returns></returns>
        Public ReadOnly Property RowNodes() As RowNodeCollection
            Get
                Return HeadNode.RowNodes
            End Get
        End Property

        ''' <summary>
        ''' 节点树的所有节点集合
        ''' </summary>
        ''' <returns></returns>
        Public ReadOnly Property RowList() As List(Of RowNode)
            Get
                Return HeadNode.RowList
            End Get
        End Property

        ''' <summary>
        ''' 节点总数
        ''' </summary>
        ''' <returns></returns>
        Public ReadOnly Property RowCount() As Integer
            Get
                Return HeadNode.AllChildCount
            End Get
        End Property


        ''' <summary>
        ''' '创建固定模块节点
        ''' </summary>
        ''' <param name="text"></param>
        ''' <param name="desc"></param>
        ''' <returns></returns>
        Private Function CreateFixedRowNode(text As String, desc As String) As RowNode
            Dim node As New RowNode With {.RowType = RowNode.RowTypeEnum.FixedModule, .Action = True, .Label = text, .Description = desc}
            node.AddNode(New RowNode()) '添加一个空的子节点
            Return node
        End Function

        ''' <summary>
        ''' 创建新的测试流程
        ''' </summary>
        Public Overrides Sub CreateStationPlan()
            HeadNode.Clear()

            HeadNode.AddNode(CreateFixedRowNode($"{FixedModuleEnum.Setup}", $"{FixedModuleEnum.Setup} Module"))
            HeadNode.AddNode(New RowNode()) '添加一个空的子节点
            HeadNode.AddNode(CreateFixedRowNode($"{FixedModuleEnum.Main}", $"{FixedModuleEnum.Main} Module"))
            HeadNode.AddNode(New RowNode()) '添加一个空的子节点
            HeadNode.AddNode(CreateFixedRowNode($"{FixedModuleEnum.Pass}", $"{FixedModuleEnum.Pass} Module"))
            HeadNode.AddNode(New RowNode()) '添加一个空的子节点
            HeadNode.AddNode(CreateFixedRowNode($"{FixedModuleEnum.Fail}", $"{FixedModuleEnum.Fail} Module"))
            HeadNode.AddNode(New RowNode()) '添加一个空的子节点
            HeadNode.AddNode(CreateFixedRowNode($"{FixedModuleEnum.Cleanup}", $"{FixedModuleEnum.Cleanup} Module"))
            HeadNode.AddNode(New RowNode()) '添加一个空的子节点
            HeadNode.AddNode(CreateFixedRowNode($"{FixedModuleEnum.Custom}", $"{FixedModuleEnum.Custom} Module"))
            HeadNode.AddNode(New RowNode()) '添加一个空的子节点
        End Sub

        ''' <summary>
        ''' 保存为Xml文件
        ''' </summary>
        ''' <param name="path"></param>
        Public Overrides Sub SaveFile(path As String)
            Dim xws As New XmlWriterSettings
            With xws
                .Indent = True
                .NewLineOnAttributes = False
                .Encoding = New UTF8Encoding(False)
            End With

            Using xw As XmlWriter = XmlWriter.Create(path, xws)
                xw.WriteStartDocument()
                xw.WriteStartElement($"UtsStationPlan") '创建跟节点

                xw.WriteStartElement($"Properties") '创建表属性节点
                '测试流程的属性
                xw.WriteEndElement()

                RowNode.SaveRowNodes(xw, HeadNode.RowNodes)

                xw.WriteEndElement()
                xw.WriteEndDocument()
            End Using
        End Sub

        ''' <summary>
        ''' 读取测试站属性
        ''' </summary>
        ''' <param name="nodeList"></param>
        Private Sub LoadProperties(nodeList As XmlNodeList)


        End Sub

        ''' <summary>
        ''' 从Xml文件加载
        ''' </summary>
        ''' <param name="path"></param>
        Public Overrides Sub LoadFile(path As String)
            Dim xd As New XmlDocument()
            xd.Load(path)

            Dim xe As XmlElement
            Dim nodeList As XmlNodeList = xd.SelectSingleNode($"UtsStationPlan").ChildNodes
            For Each node As XmlNode In nodeList
                xe = CType(node, XmlElement)
                Select Case xe.LocalName
                    Case "Properties"
                        LoadProperties(xe.ChildNodes)
                    Case "RowNodes"
                        HeadNode.Clear()
                        RowNode.LoadRowNodes(xe.ChildNodes, HeadNode.RowNodes)
                    Case Else
                        Throw New Exception($"LoadXml Unknown LocalName:{xe.LocalName}")
                End Select
            Next

        End Sub

        ''' <summary>
        ''' 测试流程固定模块
        ''' </summary>
        Enum FixedModuleEnum
            ''' <summary>
            ''' 空模块,无测试时默认值
            ''' </summary>
            None

            ''' <summary>
            ''' 主模块，测试起始模块
            ''' </summary>
            Main

            ''' <summary>
            ''' 测试通过执行模块
            ''' </summary>
            Pass

            ''' <summary>
            ''' 测试失败执行模块
            ''' </summary>
            Fail

            ''' <summary>
            ''' 测试卸载执行模块
            ''' </summary>
            Cleanup

            ''' <summary>
            ''' 测试设置执行模块
            ''' </summary>
            Setup

            ''' <summary>
            ''' 自定义模块
            ''' </summary>
            Custom
        End Enum

        'czh 2022、10、04 新加字段
        ''' <summary>
        ''' 模板文件名
        ''' </summary>
        ''' <returns></returns>
        Public Property TemplateFile As String
        ''' <summary>
        ''' 公司Logo文件
        ''' </summary>
        ''' <returns></returns>
        Public Property CompanyLogo As String

    End Class
End Namespace