using Microsoft.Win32;
using ryCommonDb;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 开发辅助工具.Manager
{
    public partial class FrmSetting : Form
    {
        public FrmSetting()
        {
            InitializeComponent();
        }

        private void BtnChapeBasePath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = TxtCshapeBasePath.Text;
            if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
            {
                TxtCshapeBasePath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if(ChkAss_Ryp.Checked)
            {
                SetFileAssociation(".ryp", "ryUSQ", "用睿元开发助手打开", Application.ExecutablePath);
            }
            else { DelFileAssociation(".ryp", "ryUSQ"); }
            if(ChkAss_Folder.Checked)
            {
                SetFileAssociation("folder", "ryUSQ", "用睿元开发助手打开", Application.ExecutablePath);
            }
            else { DelFileAssociation("folder", "ryUSQ"); }
            ryCommon.Storage mStor = new ryCommon.Storage();
            mStor.SelectNodeBySet();
            mStor.SetAttrValue("CshapeBasePath", TxtCshapeBasePath.Text);
            mStor.SetAttrValue("ReferenceDll", TxtReferenceDll.Text);
            mStor.SetAttrValue("ReactorPath", TxtReactorPath.Text);
            mStor.SetAttrValue("WinRARPath", TxtWinRARPath.Text);
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1)
            {
                RyQuickSQL mySQL = new RyQuickSQL("Settings");
                mySQL.AddField("name","Setting");
                mySQL.AddField("value", mStor.XMLText);
                if(db.ExecuteNonQuery(mySQL.GetUpdateSQL()+ " where name='Setting'",mySQL)==0)
                {
                    db.ExecuteNonQuery(mySQL.GetInsertSQL(),mySQL);
                }
            }
            db.Free();
            DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// 设置文件关联
        /// </summary>
        /// <returns></returns>
        public bool SetFileAssociation(string ExtName, string sName, string AssDes,string filePath)
        {
            try
            {
                RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot,RegistryView.Registry32);
                RegistryKey softwareRun = LRoot.OpenSubKey(ExtName + @"\shell\"+sName, true);
                if (softwareRun == null) { softwareRun = LRoot.CreateSubKey(ExtName + @"\shell\" + sName); }
                softwareRun.SetValue("", AssDes, RegistryValueKind.String);
                var Command= softwareRun.OpenSubKey("command",true);
                if (Command == null) { Command = softwareRun.CreateSubKey("command"); }
                Command.SetValue("","\""+ filePath+"\" open \"%1\"");
                Command.Close();
                softwareRun.Close();
                LRoot.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 判断文件关联是否存在
        /// </summary>
        /// <param name="ExtName"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public bool GetFileAssociation(string ExtName, string sName)
        {
            try
            {
                var result = false;
                RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32);
                RegistryKey softwareRun = LRoot.OpenSubKey(ExtName + @"\shell\"+sName, false);
                if (softwareRun != null)
                {
                    softwareRun.Close();
                    result = true;
                }
                LRoot.Close();
                return result;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 删除文件关联
        /// </summary>
        /// <param name="ExtName"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public bool DelFileAssociation(string ExtName, string sName)
        {
            try
            {
                RegistryKey LRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32);
                RegistryKey softwareRun = LRoot.OpenSubKey(ExtName + @"\shell", false);
                if(softwareRun!=null)
                {
                    var shell_name = softwareRun.OpenSubKey(sName,false);
                    if(shell_name!=null)
                    {
                        shell_name.Close();
                        softwareRun.DeleteSubKeyTree(sName);
                    }
                    softwareRun.Close();
                }
                LRoot.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        private void FrmSetting_Load(object sender, EventArgs e)
        {
            ChkAss_Ryp.Checked = GetFileAssociation(".ryp", "ryUSQ");
            ChkAss_Folder.Checked = GetFileAssociation("folder", "ryUSQ");
            DataProvider mydb = new DataProvider();
            IDbInterface db = Itrycn_Db.CreateDataProvider(Itrycn_Db.dataType);
            if (db.ConnDb(Itrycn_Db.User_SQLConn) == 1)
            {
                var ds = db.ReadData("select * from Settings where name='Setting'");
                if(mydb.HaveData(ds))
                {
                    var row = mydb.GetData(ds);
                    ryCommon.Storage mStor = new ryCommon.Storage(row["value"].ToString());
                    mStor.SelectNodeBySet();
                    TxtCshapeBasePath.Text = mStor.GetAttrValue("CshapeBasePath");
                    TxtReferenceDll.Text = mStor.GetAttrValue("ReferenceDll");
                    TxtReactorPath.Text = mStor.GetAttrValue("ReactorPath");
                    TxtWinRARPath.Text = mStor.GetAttrValue("WinRARPath");
                    if(TxtWinRARPath.Text=="")
                    {
                        if(System.IO.File.Exists(@"C:\Program Files\WinRAR\WinRAR.exe"))
                        {
                            TxtWinRARPath.Text = @"C:\Program Files\WinRAR\WinRAR.exe";
                        }
                    }
                }
            }
            db.Free();
        }

        private void BtnReactorPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = TxtReactorPath.Text;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                TxtReactorPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void BtnWinRARPath_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = TxtWinRARPath.Text;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                TxtWinRARPath.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
