using System;
using System.Windows.Controls;
using NavisworksTransport.UI.WPF.ViewModels;

namespace NavisworksTransport.UI.WPF.Views
{
    /// <summary>
    /// AnimationControlView.xaml 的交互逻辑
    /// 检测动画页签视图，提供动画控制和碰撞检测功能
    /// </summary>
    public partial class AnimationControlView : UserControl
    {
        /// <summary>
        /// 动画控制视图模型
        /// </summary>
        public AnimationControlViewModel ViewModel { get; private set; }

        public AnimationControlView() : this(null)
        {
            // 调用带参数的构造函数，传入null
        }

        /// <summary>
        /// 带主ViewModel参数的构造函数，支持统一状态栏
        /// </summary>
        /// <param name="mainViewModel">主ViewModel，用于统一状态栏</param>
        public AnimationControlView(LogisticsControlViewModel mainViewModel)
        {
            InitializeComponent();
            InitializeViewModel(mainViewModel);
        }

        /// <summary>
        /// 初始化ViewModel
        /// </summary>
        private void InitializeViewModel()
        {
            try
            {
                ViewModel = new AnimationControlViewModel();
                DataContext = ViewModel;
                LogManager.Info("AnimationControlView ViewModel初始化完成");
            }
            catch (Exception ex)
            {
                LogManager.Error($"AnimationControlView ViewModel初始化失败: {ex.Message}", ex);
                throw;
            }
        }

        /// <summary>
        /// 初始化ViewModel（支持统一状态栏）
        /// </summary>
        /// <param name="mainViewModel">主ViewModel，用于统一状态栏</param>
        private void InitializeViewModel(LogisticsControlViewModel mainViewModel)
        {
            try
            {
                ViewModel = new AnimationControlViewModel(mainViewModel);
                DataContext = ViewModel;
                LogManager.Info("AnimationControlView ViewModel初始化完成 - 支持统一状态栏");
            }
            catch (Exception ex)
            {
                LogManager.Error($"AnimationControlView ViewModel初始化失败: {ex.Message}", ex);
                throw;
            }
        }

        /// <summary>
        /// 清理资源
        /// </summary>
        public void Cleanup()
        {
            try
            {
                ViewModel?.Dispose();
                LogManager.Info("AnimationControlView资源清理完成");
            }
            catch (Exception ex)
            {
                LogManager.Error($"AnimationControlView资源清理失败: {ex.Message}");
            }
        }
    }
}