Imports System.Windows.Forms

Namespace UTSModule.Project
    Public Class FrmStationDesign

        Private _isAutoAugment As Boolean = True

        Public Property StationPacket() As ProjectStationPacket


        Public Sub ShowForm(parentControl As Control)
            FormBorderStyle = FormBorderStyle.None
            TopLevel = False
            Dock = DockStyle.Fill
            Parent = parentControl

            Enabled = StationPacket IsNot Nothing

            Show()
        End Sub

        Public Sub ShowForm(parentControl As Control, packet As ProjectStationPacket)
            FormBorderStyle = FormBorderStyle.None
            TopLevel = False
            Dock = DockStyle.Fill
            Parent = parentControl

            UpdateStationPacket(packet)
            Enabled = StationPacket IsNot Nothing
            Show()
        End Sub

        Public Sub Station_Changed(station As StationInfo)
            UpdateStationPacket(station.Packet)
        End Sub


        ''' <summary>修改窗体标题</summary>
        Private Sub ShowFormTitle()
            Text = $"{My.Application.Info.ProductName} StationDesign"
        End Sub

        Private Sub ShowFormTitle(packetName As String)
            Text = $"{My.Application.Info.ProductName} StationDesign -- {packetName}"
        End Sub


        Private Sub UpdateStationPacket(packet As ProjectStationPacket)
            StationPacket = packet
            Enabled = StationPacket IsNot Nothing
            If StationPacket IsNot Nothing Then
                ShowFormTitle(StationPacket.Name)
                LoadProjectInitForm()
            End If
        End Sub


        Private Sub LoadProjectInitForm()
            TxtProjectName.Text = StationPacket.ParentStation.ParentProject.Name
            TxtTestStation.Text = StationPacket.ParentStation.Name
            TxtEditPwd.Text = StationPacket.PassWord


            NudStationVersion.Text = CType(StationPacket.StationVersion, String)
            PicStation.Image = StationPacket.StationImage
            RtxHistoryImprint.Text = ProjectStationPacket.PacketImprintsToString(StationPacket.HistoryImprints)
            RtxCurrentImprint.Text = StationPacket.CurrentImprint.ToString()

            If StationPacket.ValidDate >= DtpValidDate.MaxDate Then
                DtpValidDate.Value = Now.AddMonths(1) '默认有效期一个月
            ElseIf StationPacket.ValidDate <= DtpValidDate.MinDate Then
                DtpValidDate.Value = Now.AddMonths(1) '默认有效期一个月
            Else
                DtpValidDate.Value = StationPacket.ValidDate
            End If
        End Sub


        Private Sub InitForm()
            ShowFormTitle()
        End Sub

        Private Sub FrmStationDesign_Load(sender As Object, e As EventArgs) Handles Me.Load
            InitForm()
        End Sub


        Private Sub PicStation_DoubleClick(sender As Object, e As EventArgs) Handles PicStation.DoubleClick
            Using dlgOpenFile As New OpenFileDialog
                dlgOpenFile.Filter = $"设备图像 (*.bmp;*.gif;*.jpg;*.png)|*.bmp;*.gif;*.jpg;*.png"
                If dlgOpenFile.ShowDialog = DialogResult.OK Then
                    Try
                        Dim imagePath As String = dlgOpenFile.FileName
                        PicStation.Image = ImageProcessor.ImageProcessor.GetBitmapImage(imagePath)

                        '更新图像预览图
                        If String.IsNullOrEmpty(StationPacket.ImageFileName) Then '若图像路径不存在,则更新图像路径
                            Dim imgSavePath As String = $"{UtsPath.StationPacketResourceDirPath(StationPacket.Name)}\{StationPacket.ParentStation.Index}.jpg"
                            PicStation.Image.Save(imgSavePath)

                            StationPacket.ImageFileName = $"{StationPacket.ParentStation.Index}.jpg" '更新站图像路径
                        Else
                            Dim imgSavePath As String = $"{UtsPath.StationPacketResourceDirPath(StationPacket.Name)}\{StationPacket.ImageFileName}"
                            PicStation.Image.Save(imgSavePath)
                        End If
                    Catch ex As Exception
                        MsgBox($"设置项目图像失败，{ex.Message}")
                    End Try
                End If
            End Using
        End Sub


        Private Sub UpdateStationPacket()
            StationPacket.ModifiedTime = Now
            StationPacket.PassWord = TxtEditPwd.Text
            StationPacket.ValidDate = DtpValidDate.Value
            StationPacket.CurrentImprint = New StationPacketImprint(RtxCurrentImprint.Text)
            If _isAutoAugment Then
                StationPacket.StationVersion += 1
            Else
                StationPacket.StationVersion = CInt(NudStationVersion.Value)
            End If
        End Sub

        Private Sub AfterReleasePacketSuccess()
            RtxCurrentImprint.Text = StationPacket.CurrentImprint.ToString()
            RtxHistoryImprint.Text = ProjectStationPacket.PacketImprintsToString(StationPacket.HistoryImprints)
            NudStationVersion.Value = StationPacket.StationVersion

            ShowFormTitle(StationPacket.Name)
        End Sub

        Private Sub AfterReleasePacketFail()
            RtxCurrentImprint.Text = StationPacket.CurrentImprint.ToString()
            RtxHistoryImprint.Text = ProjectStationPacket.PacketImprintsToString(StationPacket.HistoryImprints)
            NudStationVersion.Value = StationPacket.StationVersion

            ShowFormTitle(StationPacket.Name)
        End Sub

        Private Sub TsBtnReleaseStation_Click(sender As Object, e As EventArgs) Handles TsBtnReleaseStation.Click
            Try
                UpdateStationPacket()
                StationPacket.ReleasePacket()
                AfterReleasePacketSuccess()

            '    RaiseEvent ReleaseStationSuccessed(StationPacket.ParentStation)
                MsgBox($"项目站包 {StationPacket.FileName} 发布成功!")
            Catch ex As Exception
                MsgBox($"项目站包发布失败,{ex.Message}")

                AfterReleasePacketFail()
            End Try
        End Sub

        Private Sub ChkAutoAugment_CheckedChanged(sender As Object, e As EventArgs) Handles ChkAutoAugment.CheckedChanged
            _isAutoAugment = ChkAutoAugment.Checked
            NudStationVersion.Enabled = Not _isAutoAugment
        End Sub
    End Class
End Namespace