Imports System.Drawing
Imports System.Windows.Forms
Imports UTS_Core.UTSModule.Project

Namespace UTSModule.Station
    Public Class DlgLoadStation
        Public Property ProcessStation() As ProcessStation

        Public Property UserInfo() As Login.UserInfo

        Public _projectInfo As ProjectInfo

        Private _projectName As String

        Private _stationName As String

        Private _stationType As ProcessStation.StationTypeEnum

        Private _snIndex As New Dictionary(Of Integer, Integer)

        Public Function InitProjectStationWithoutShow(projectName As String, stationName As String) As Boolean
            Dim projectList As String() = ProjectInfo.LoadProjectList(ProjectInfo.InitializeModeEnum.LocalDatabaseLoad)
            If projectList.Contains(projectName) = False Then Return False

            _projectInfo = New ProjectInfo(UserInfo.UserId, UserInfo.UserName, projectName, ProjectInfo.InitializeModeEnum.LocalDatabaseLoad)
            For Each projectStationInfo As ProcessStation In _projectInfo.Station
                If projectStationInfo.Name = stationName Then
                    ProcessStation = projectStationInfo
                    ProcessStation.UserId = UserInfo.UserId
                    Exit For
                End If
            Next

            Return ProcessStation IsNot Nothing
        End Function

        Public Overloads Function ShowDialog(projectName As String, stationName As String, Optional stationType As ProcessStation.StationTypeEnum = ProcessStation.StationTypeEnum.Test) As DialogResult
            _projectName = projectName
            _stationName = stationName
            _stationType = stationType
            Return Me.ShowDialog
        End Function

        Private Sub OK_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OK_Button.Click
            DialogResult = DialogResult.OK
            Close()
        End Sub

        Private Sub Cancel_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Cancel_Button.Click
            DialogResult = DialogResult.Cancel
            Close()
        End Sub

        Private Sub InitPreviewImage()
            PicStationPreview.SizeMode = PictureBoxSizeMode.Zoom
        End Sub

        Private Sub InitializeForm()
            Text = $"请选择需要加载的项目"
        End Sub

        Private Sub DlgLoadStation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            InitializeForm()
            InitPreviewImage()
            UpdateProjectList()
        End Sub

        Private Sub UpdateProjectList()
            CboProject.Items.Clear()
            CboProject.Items.AddRange(ProjectInfo.LoadProjectList(ProjectInfo.InitializeModeEnum.LocalDatabaseLoad))

            If String.IsNullOrEmpty(_projectName) = False Then
                For i As Integer = 0 To CboProject.Items.Count - 1
                    If CboProject.Items(i).ToString() = _projectName Then
                        CboProject.SelectedIndex = i
                        Exit For
                    End If
                Next
            Else
                If CboProject.Items.Count > 0 Then CboProject.SelectedIndex = 0
            End If
        End Sub

        Private Sub UpdateProjectStation(stations As List(Of ProcessStation))
            _snIndex.Clear()
            CboStation.Items.Clear()

            Dim index As Integer = -1
            For Each station As ProcessStation In stations
                index += 1

                If station.StationType <> _stationType Then Continue For

                _snIndex.Add(CboStation.Items.Count, index)
                CboStation.Items.Add(station.Name)
            Next

            CboPacket.Items.Clear()


            '添加所有站位描述
            RtxStationDesc.SuspendLayout()
            RtxStationDesc.Clear()
            For Each station As ProcessStation In stations
                If station.StationType <> _stationType Then
                    RtxStationDesc.SelectionColor = Color.Gray
                Else
                    RtxStationDesc.SelectionColor = Color.Black
                End If
                RtxStationDesc.AppendText($"{station.Name}:{station.Description}{vbNewLine}")
            Next
            RtxStationDesc.ResumeLayout(False)
            RtxStationDesc.PerformLayout()

            '默认选择
            If String.IsNullOrEmpty(_stationName) = False Then
                For i As Integer = 0 To CboStation.Items.Count - 1
                    If CboStation.Items(i).ToString() = _stationName Then
                        CboStation.SelectedIndex = i
                        Exit For
                    End If
                Next
            End If

            If CboStation.SelectedIndex = -1 AndAlso _snIndex.Count > 0 Then CboStation.SelectedIndex = _snIndex.Keys.ElementAt(0)
        End Sub

        Private Sub CboProject_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboProject.SelectedIndexChanged
            _projectInfo = New ProjectInfo(UserInfo.UserId, UserInfo.UserName, CboProject.Text, ProjectInfo.InitializeModeEnum.LocalDatabaseLoad)

            RtxProjectDesc.Text = _projectInfo.Description

            If _projectInfo.MasterImage IsNot Nothing Then
                PicStationPreview.Image = _projectInfo.MasterImage
            End If

            lastSelectIndex = -1
            UpdateProjectStation(_projectInfo.Station)
        End Sub

        Private lastSelectIndex As Integer = -1
        Private _packetList As List(Of StationPacketVo)

        Private Sub CboStation_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboStation.SelectedIndexChanged
            Dim selectIndex As Integer = _snIndex(CboStation.SelectedIndex)
            ProcessStation = _projectInfo.Station.Item(selectIndex)
            ProcessStation.UserId = UserInfo.UserId

            If lastSelectIndex <> -1 Then
                RtxStationDesc.Select(RtxStationDesc.GetFirstCharIndexFromLine(lastSelectIndex), RtxStationDesc.Lines(lastSelectIndex).Length)
                RtxStationDesc.SelectionColor = Color.Black
            End If
            lastSelectIndex = selectIndex

            RtxStationDesc.Select(RtxStationDesc.GetFirstCharIndexFromLine(selectIndex), RtxStationDesc.Lines(selectIndex).Length)
            RtxStationDesc.SelectionColor = Color.Blue


            '获取pid和sid
            Dim pid As Integer = _projectInfo.Index
            Dim sid As Integer = ProcessStation.StationID

            '获取对应站位所有包，倒序添加
            _packetList = _projectInfo.LoadLocalDbStationPackets(pid, sid)
            CboPacket.Items.Clear()
            For Each packet As StationPacketVo In _packetList
                CboPacket.Items.Add(packet.PacketName)
            Next

            If CboPacket.Items.Count > 0 Then CboPacket.SelectedIndex = 0
        End Sub

        Private Sub CboPacket_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboPacket.SelectedIndexChanged
            ProcessStation.Packet.FileName = _packetList(CboPacket.SelectedIndex).PacketName
            ProcessStation.Packet.PacketMD5 = _packetList(CboPacket.SelectedIndex).PacketMd5
        End Sub
    End Class
End Namespace
