using CommunityToolkit.Mvvm.Input;
using Microsoft.Win32;
using Misc;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace FLY.Thick.Base.UI.CustomSection
{
    /// <summary>
    /// UcSectionWarning.xaml 的交互逻辑
    /// </summary>
    public partial class UcSectionError : UserControl
    {
        UcSectionErrorVm viewModel;

        public UcSectionError()
        {
            InitializeComponent();
        }

        [Unity.InjectionMethod]
        public void Init(ParamDictionary paramDictionary)
        {
            viewModel = new UcSectionErrorVm();
            viewModel.Init(paramDictionary);
            this.DataContext = viewModel;
        }
    }
    public class UcSectionErrorVm : INotifyPropertyChanged
    {
        /// <summary>
        /// 报警音乐路径 
        /// </summary>
        public string WarningTipPath { get; set; }

        /// <summary>
        /// 报警持续时间 单位:秒
        /// </summary>
        public int WarningDurationSec { get; set; }

        /// <summary>
        /// 大屏幕提示扫描报警 
        /// </summary>
        public bool EnableScanErrBigTip { get; set; }


        public event PropertyChangedEventHandler PropertyChanged;


        public RelayCommand PlayCmd { get; }
        public RelayCommand OpenCmd { get; }


        ParamDictionary paramDictionary;

        public UcSectionErrorVm()
        {
            PlayCmd = new RelayCommand(Play);
            OpenCmd = new RelayCommand(Open);

        }

        public void Init(ParamDictionary paramDictionary)
        {
            this.paramDictionary = paramDictionary;

            paramDictionary.SetBinding(this, nameof(WarningTipPath), ParamDistItemKeys.WarningTipPath, "");
            paramDictionary.SetBinding(this, nameof(WarningDurationSec), ParamDistItemKeys.WarningDurationSec, 5);
            paramDictionary.SetBinding(this, nameof(EnableScanErrBigTip), ParamDistItemKeys.EnableScanErrBigTip, false);
        }

        private void Play()
        {
            //报警!!!!!!!!
            FLY.ControlLibrary.Window_WarningTip.Show("TEST",WarningTipPath + $" {WarningDurationSec}s",TimeSpan.FromSeconds(WarningDurationSec),
                WarningTipPath);
        }

        private void Open()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "mp3|*.mp3|wav|*.wav|any|*.*";

            string tit = (string)Application.Current.TryFindResource("str.PgCustomSections.SelectMusicFile");
            ofd.Title = tit;

            if (System.IO.Path.IsPathRooted(WarningTipPath))
                ofd.InitialDirectory = System.IO.Path.GetDirectoryName(WarningTipPath);
            else
                ofd.InitialDirectory = System.Environment.CurrentDirectory;

            if (ofd.ShowDialog() == true)
            {
                string currentDirectory = System.Environment.CurrentDirectory.ToLower();
                string filename = ofd.FileName.ToLower();

                if (filename.StartsWith(currentDirectory))
                {
                    //CurrentDirectory没有最后的\ 需要+1
                    WarningTipPath = ofd.FileName.Substring(System.Environment.CurrentDirectory.Length + 1);
                }
                else
                {
                    WarningTipPath = ofd.FileName;
                }
            }
        }

    }
}