using MeterVision.model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MeterVision.Dlg
{
    /// <summary>
    /// CompLogViewer.xaml 的交互逻辑
    /// </summary>
    public partial class CompLogViewer : Window
    {
        private readonly CompDetailItem DetailItem;

        public CompLogViewer(CompDetailItem detailItem)
        {
            InitializeComponent();

            DetailItem = detailItem;
            InitLoad(detailItem);
        }

        private void InitLoad(CompDetailItem detailItem)
        {
            ucImageSrc.ImageName = "新任务结果图片";
            ucImageDst.ImageName = "旧任务结果图片";

            if (File.Exists(detailItem.Detail1.DstImage))
            {
                ucImageSrc.ImageSource = detailItem.Detail1.DstImage;
            }

            if (File.Exists(detailItem.Detail2.DstImage))
            {
                ucImageDst.ImageSource = detailItem.Detail2.DstImage;
            }

            if (File.Exists(detailItem.Detail1.LogPath))
            {
                LoadLogContent(RichTxtContentBox,detailItem.Detail1.LogPath);
            }
            if (File.Exists(detailItem.Detail2.LogPath))
            {
                LoadLogContent(RichTxtContentBox2,detailItem.Detail2.LogPath);
            }

        }

        private void LoadLogContent(RichTextBox richTextBox,string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    string content = File.ReadAllText(filePath);

                    // 创建段落并将日志内容添加到段落
                    Paragraph logParagraph = new Paragraph(new Run(content));

                    // 将日志段落添加到 RichTextBox 的文档中
                    //RichTxtContentBox.Document.Blocks.Add(logParagraph);
                    richTextBox.Document.Blocks.Add(logParagraph);
                }
                else
                {
                    MessageBox.Show("日志文件未找到！", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载日志文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {

        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void LogScrollTop_Click(object sender, RoutedEventArgs e)
        {
            RichTxtContentBox.ScrollToHome();
        }

        private void LogScrollBottom_Click(object sender, RoutedEventArgs e)
        {
            RichTxtContentBox.ScrollToEnd();
        }

        private void AiLogSave_Click(object sender, RoutedEventArgs e)
        {
            if (DetailItem == null) return;

            string logPath = DetailItem.Detail1.LogPath;

            if (!string.IsNullOrEmpty(logPath) && File.Exists(logPath))
            {
                ThisApp.ExportAiLog(logPath);
            }
        }

        private void LogScrollTop2_Click(object sender, RoutedEventArgs e)
        {
            RichTxtContentBox2.ScrollToHome();
        }

        private void LogScrollBottom2_Click(object sender, RoutedEventArgs e)
        {
            RichTxtContentBox2.ScrollToEnd();
        }

        private void AiLogSave2_Click(object sender, RoutedEventArgs e)
        {
            if (DetailItem == null) return;

            string logPath = DetailItem.Detail2.LogPath;

            if (!string.IsNullOrEmpty(logPath) && File.Exists(logPath))
            {
                ThisApp.ExportAiLog(logPath);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {

        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {

        }


        //---------------------------------------------------------
    }
}
