/*-------------------------------------------------------------------------
 * 功能描述：SingleInstance
 * 作者：xulisong
 * 创建时间： 2018/12/21 9:01:34
 * 版本号：v1.0
 *  -------------------------------------------------------------------------*/

using FWindSoft.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Com.FirmLib.UI
{
    public class WindowSingleInstance<T> where T:Window
    {
        private readonly Func<T> m_CreateInstance;
        public WindowSingleInstance()
        {
            m_CreateInstance = CreateInstance;
        }
        public WindowSingleInstance(Func<T> createInstance)
        {
            m_CreateInstance = createInstance;
        }

        protected T CreateInstance()
        {
            Type type = typeof(T);
            ConstructorInfo  construct = type.GetConstructor(Type.EmptyTypes);
            if (construct == null)
                return default(T);
            return (T)construct.Invoke(null);
        }
        protected T m_Target;
        /// <summary>
        /// 目标信息
        /// </summary>
        public virtual T Target
        {
            get
            {
                if (m_Target == null||m_Target.IsDisposed())
                {
                    if (m_CreateInstance!=null)
                    {
                        m_Target = m_CreateInstance();
                    }                  
                }
                else
                {
                    m_Target.Visibility = Visibility.Visible;
                    m_Target.Activate();
                }
                return m_Target;
            }
        }      
    }  
}
