using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;

namespace LiveUpdate
{
    /// <summary>
    /// 
    /// </summary>
    public class RyUpdate
    {
        /// <summary>
        /// 
        /// </summary>
        public ClsLiveUpdate myLiveUpdate = new ClsLiveUpdate(null);
        /// <summary>
        /// 
        /// </summary>
        public static Dictionary<string, string> dict_downlist = new Dictionary<string, string>();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        public RyUpdate(string url)
        {
            myLiveUpdate.OnUpdateComplete += OnUpdateComplete;
            myLiveUpdate.OnNoUpdate += MyLiveUpdate_OnNoUpdate;
            myLiveUpdate.UpdateUrl = url;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_ParentForm"></param>
        /// <param name="url"></param>
        public RyUpdate(Form _ParentForm, string url)
        {
            ParentForm = _ParentForm;
            myLiveUpdate.OnUpdateComplete += OnUpdateComplete;
            myLiveUpdate.OnNoUpdate += MyLiveUpdate_OnNoUpdate;
            myLiveUpdate.UpdateUrl = url;
        }
        /// <summary>
        /// 
        /// </summary>
        ~RyUpdate()
        {
            myLiveUpdate.OnUpdateComplete -= OnUpdateComplete;
            myLiveUpdate.OnNoUpdate -= MyLiveUpdate_OnNoUpdate;
        }
        /// <summary>
        /// 当前登陆用户
        /// </summary>
        public string CurUserId
        {
            get { return myLiveUpdate.CurUserId; }
            set { myLiveUpdate.CurUserId = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public Form ParentForm
        {
            get { return myLiveUpdate.ParentForm; }
            set { myLiveUpdate.ParentForm = value; }
        }
        /// <summary>
        /// 没有版本更新时激发
        /// </summary>
        [Description("没有版本更新时激发")]
        public event EventHandler OnNoUpdate;
        private void MyLiveUpdate_OnNoUpdate(object sender, EventArgs e)
        {
            OnNoUpdate?.Invoke(this, new EventArgs());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool CheckUpdate()
        {
            //LiveUpdate.clsUpdateInfo.myLiveUpdate.SettingPath = Application.StartupPath + @"\cUpdate.dat";
            myLiveUpdate.StartLiveUpdate();
            return true;
        }
        /// <summary>
        /// 取消更新。
        /// </summary>
        public event EventHandler OnUpdateCancel;
        private void OnUpdateComplete(LiveUpdate.UpdateInfo e)
        {
            try
            {
                for (int i = 0; i < Application.OpenForms.Count; i++)
                {
                    if (Application.OpenForms[i] is LiveUpdate.FrmUpdate || Application.OpenForms[i] is LiveUpdate.frmStartUpdate)
                    { return; }
                }
            }
            catch { }
            LiveUpdate.FrmUpdate frm = new LiveUpdate.FrmUpdate(myLiveUpdate);
            frm.OnAppExit += Frm_OnAppExit;
            frm.SetUpdateInfo(e);
            if(frm.ShowDialog()==DialogResult.Cancel)
            {
                OnUpdateCancel?.Invoke(this,new EventArgs());
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public event LiveUpdate.OnAppExit OnAppExit;
        private void Frm_OnAppExit(object sender, EventArgs e)
        {
           OnAppExit?.Invoke(sender, e);
        }
    }
}
