Imports System.Drawing
Imports System.Windows.Forms

Namespace UTSModule.Project
    Public Class DlgLoadStation
        Public Property StationInfo() As StationInfo
        Public Property UserInfo() As Login.UserInfo

        Private _projectInfo As ProjectInfo

        Private _projectName As String

        Private _stationName As String

        Public Function InitProjectStationInfoWithoutShow(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, projectName, ProjectInfo.InitializeModeEnum.LocalDatabaseLoad)
            For Each projectStationInfo As StationInfo In _projectInfo.Station
                If projectStationInfo.Name = stationName Then
                    StationInfo = projectStationInfo
                    StationInfo.UserId = UserInfo.UserId
                    Exit For
                End If
            Next

            Return StationInfo IsNot Nothing
        End Function

        Public Overloads Function ShowDialog(projectName As String, stationName As String) As DialogResult
            _projectName = projectName
            _stationName = stationName
            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.StretchImage
        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 StationInfo))
            CboStation.Items.Clear()

            For Each station As StationInfo In stations
                CboStation.Items.Add(station.Name)
            Next


            '添加所有站位描述
            RtxStationDesc.SuspendLayout()
            RtxStationDesc.Clear()
            For Each station As StationInfo In stations
                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
            Else
                If CboStation.Items.Count > 0 Then CboStation.SelectedIndex = 0
            End If
        End Sub

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

            RtxProjectDesc.Text = _projectInfo.Description

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

            UpdateProjectStation(_projectInfo.Station)
        End Sub


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

            Static lastSelectIndex As Integer = 0
            RtxStationDesc.Select(RtxStationDesc.GetFirstCharIndexFromLine(lastSelectIndex), RtxStationDesc.Lines(lastSelectIndex).Length)
            RtxStationDesc.SelectionColor = Color.Gray
            lastSelectIndex = selectIndex


            RtxStationDesc.Select(RtxStationDesc.GetFirstCharIndexFromLine(selectIndex), RtxStationDesc.Lines(selectIndex).Length)
            RtxStationDesc.SelectionColor = Color.Blue
        End Sub
    End Class
End Namespace
