using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
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.Navigation;
using System.Windows.Shapes;

namespace BPASmartClient.Control
{
    /// <summary>
    /// VersionView.xaml 的交互逻辑
    /// </summary>
    public partial class VersionView :UserControl
    {
        public VersionView()
        {
            InitializeComponent();
        }
        ReaderWriterLockSlim m_lock = new ReaderWriterLockSlim();
        string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}/whatnew.txt";

        /// <summary>
        /// 加载文件
        /// </summary>
        private void UserControl_Loaded(object sender,RoutedEventArgs e)
        {
            try
            {
                //通过一体化启动时 路径为
                if (File.Exists(path))
                {
                    try
                    {
                        m_lock.EnterReadLock();
                        using (StreamReader sr = new StreamReader(path,Encoding.Default))
                        {
                            string str = sr.ReadToEnd();
                            if (!string.IsNullOrEmpty(str))
                            {
                                string[] content = str.Split(new char[] { '\r','\n' });

                                this.memoEdit1.Text = str;
                                if (content.Length > 3)
                                    label_currentver.Content = "当前版本:" + content[0];
                                else
                                {
                                    this.label_currentver.Content = "当前版本:N/A";
                                }
                            }
                            else
                            {
                                this.memoEdit1.Text = "没有任何内容";
                                label_currentver.Content = "当前版本:N/A";
                            }
                        }
                        m_lock.ExitReadLock();
                    }
                    catch
                    {
                        this.memoEdit1.Text = "版本文件加载错误";
                        label_currentver.Content = "当前版本:N/A";
                    }
                    finally
                    {
                        if (m_lock.IsReadLockHeld) m_lock.ExitReadLock();
                    }
                }
                else
                {
                    WriteFile(path,this.memoEdit1.Text);
                    this.memoEdit1.Text = "无版本描述文件";
                    label_currentver.Content = "当前版本:N/A";
                }

            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender,RoutedEventArgs e)
        {
            WriteFile(path,this.memoEdit1.Text);
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <param name="Strings">文件内容</param>
        public static void WriteFile(string Path,string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
                f.Dispose();
            }

            System.IO.StreamWriter f2 = File.CreateText(Path);
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }
    }
}
