using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Net;

namespace ProjectSUConfig
{
    public partial class Form1 : Form
    {
        string SetingPath = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf(@"\"))+"\\Seting\\Seting.ini";

        private static byte[] getBytes(string s, string encodingName)
        {
            return null == s ? null : Encoding.GetEncoding(encodingName).GetBytes(s);
        }
        public static string GetConfig(string section, string key, string def, string fileName, string encodingName = "utf-8", int size = 1024)
        {
            byte[] buffer = new byte[size];
            int count = GetPrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(def, encodingName), buffer, size, fileName);
            return Encoding.GetEncoding(encodingName).GetString(buffer, 0, count).Trim();
        }
        public static bool SetConfig(string section, string key, string value, string fileName, string encodingName = "utf-8")
        {
            return WritePrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(value, encodingName), fileName);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ReadConfig();
            LocalIP.Text = GetLocalIP();
            comboBox1.SelectedIndex = 0;
        }

        [DllImport("kernel32")]
        public static extern bool WritePrivateProfileString(byte[] section, byte[] key, byte[] val, string filePath);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(byte[] section, byte[] key, byte[] def, byte[] retVal, int size, string filePath);

        public void ReadConfig() {
            NetDomain.Text = GetConfig("Net", "Domain", "", SetingPath);
            NetPort.Text = GetConfig("Net", "Port", "", SetingPath);

            IndexIETitle.Text = GetConfig("Index", "Title", "", SetingPath);
            IndexTitle.Text = GetConfig("Index", "PageTitle", "", SetingPath);
            IndexTips.Text = GetConfig("Index", "Tips", "", SetingPath);
            IndexAbout1.Text = GetConfig("Index", "About1", "", SetingPath);
            IndexAbout2.Text = GetConfig("Index", "About2", "", SetingPath);
            IndexReg.Text = GetConfig("Index", "RegName", "", SetingPath);
            IndexLogin.Text = GetConfig("Index", "LoginName", "", SetingPath);

            RegIETitle.Text = GetConfig("Reg", "Title", "", SetingPath);
            RegTitle.Text = GetConfig("Reg", "PageTitle", "", SetingPath);
            RegAbout1.Text = GetConfig("Reg", "About1", "", SetingPath);
            RegAbout2.Text = GetConfig("Reg", "About2", "", SetingPath);
            RegNumberName.Text = GetConfig("Reg", "MoNumberName", "", SetingPath);
            RegNameName.Text = GetConfig("Reg", "MoNameName", "", SetingPath);
            RegPWName.Text = GetConfig("Reg", "MoPasswordName", "", SetingPath);

            RegPIETitle.Text = GetConfig("RegPost", "Title", "", SetingPath);
            RegPTitle.Text = GetConfig("RegPost", "PageTitle", "", SetingPath);
            RegPAbout1.Text = GetConfig("RegPost", "About1", "", SetingPath);
            RegPAbout2.Text = GetConfig("RegPost", "About2", "", SetingPath);
            RegPNone.Text = GetConfig("RegPost", "TipsNone", "", SetingPath);
            RegPExists.Text = GetConfig("RegPost", "TipsExists", "", SetingPath);
            RegPSucc.Text = GetConfig("RegPost", "TipsSuccess", "", SetingPath);
            RegPNo.Text = GetConfig("RegPost", "TipsNo", "", SetingPath);

            LoginIETtile.Text = GetConfig("Login", "Title", "", SetingPath);
            LoginTitle.Text = GetConfig("Login", "PageTitle", "", SetingPath);
            LoginAbout1.Text = GetConfig("Login", "About1", "", SetingPath);
            LoginAbout2.Text = GetConfig("Login", "About2", "", SetingPath);

            LoginPIETitle.Text = GetConfig("LoginPost","Title", "", SetingPath);
            LoginPTitle.Text = GetConfig("LoginPost", "PageTitle", "", SetingPath);
            LoginPAbout1.Text = GetConfig("LoginPost", "About1", "", SetingPath);
            LoginPAbout2.Text = GetConfig("LoginPost", "About2", "", SetingPath);
            LoginPNone.Text = GetConfig("LoginPost", "TipsNone", "", SetingPath);
            LoginPNoExists.Text = GetConfig("LoginPost", "TipsNoExists", "", SetingPath);
            LoginPErrPW.Text = GetConfig("LoginPost", "TipsErrPW", "", SetingPath);

            MPageIETitle.Text = GetConfig("MainPage", "Title", "", SetingPath);
            MPageTitle.Text = GetConfig("MainPage", "PageTitle", "", SetingPath);
            MPageAbout1.Text = GetConfig("MainPage", "About1", "", SetingPath);
            MPageAbout2.Text = GetConfig("MainPage", "About2", "", SetingPath);

            CPageIETitle.Text = GetConfig("ChangePage", "Title", "", SetingPath);
            CPageTitle.Text = GetConfig("ChangePage", "PageTitle", "", SetingPath);
            CPageAbout1.Text = GetConfig("ChangePage", "About1", "", SetingPath);
            CPageAbout2.Text = GetConfig("ChangePage", "About2", "", SetingPath);
            CPageNo.Text = GetConfig("ChangePage", "TipsNo", "", SetingPath);
            CPageNoNo.Text = GetConfig("ChangePage", "TipsNoNo", "", SetingPath);
            CPageSucc.Text = GetConfig("ChangePage", "TipsSuccess", "", SetingPath);

            APageIETitle.Text = GetConfig("AdminPage", "Title", "", SetingPath);
            APageTitle.Text = GetConfig("AdminPage", "PageTitle", "", SetingPath);
            APageAbout1.Text = GetConfig("AdminPage", "About1", "", SetingPath);
            APageAbout2.Text = GetConfig("AdminPage", "About2", "", SetingPath);
            APagePath.Text = GetConfig("Admin", "Path", "", SetingPath);

            APPageIETitle.Text = GetConfig("AdminPost", "Title", "", SetingPath);
            APPageTitle.Text = GetConfig("AdminPost", "PageTitle", "", SetingPath);
            APPageAbout1.Text = GetConfig("AdminPost", "About1", "", SetingPath);
            APPageAbout2.Text = GetConfig("AdminPost", "About2", "", SetingPath);
            APPageListName1.Text = GetConfig("AdminPost", "ListName1", "", SetingPath);
            APPageListName2.Text = GetConfig("AdminPost", "ListName2", "", SetingPath);

            AdminName.Text = GetConfig("Admin","Name","",SetingPath);

            this.listView1.Items.Clear();
            int Index = 0;
            for (; ; ) {
                string GetConf = GetConfig("Main", "pa" + Index.ToString(), "", SetingPath);
                if (GetConf != "")
                {
                    if (GetConf.Substring(0, 3) == "str") {
                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = "文本";
                        lvi.SubItems.Add(GetConf.Substring(4,GetConf.Length-4));
                        listView1.Items.Add(lvi);
                    }
                    else if (GetConf.Substring(0, 3) == "int") {
                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = "数字";
                        lvi.SubItems.Add(GetConf.Substring(4, GetConf.Length - 4));
                        listView1.Items.Add(lvi);
                    }
                }
                else {
                    break;
                }
                Index++;
            }
        }

        public void WriteConfig()
        {
            SetConfig("Net", "Domain", NetDomain.Text, SetingPath);
            SetConfig("Net", "Port", NetPort.Text, SetingPath);

            SetConfig("Index", "Title",IndexIETitle.Text, SetingPath);
            SetConfig("Index", "PageTitle", IndexTitle.Text, SetingPath);
            SetConfig("Index", "Tips",IndexTips.Text, SetingPath);
            SetConfig("Index", "About1",IndexAbout1.Text, SetingPath);
            SetConfig("Index", "About2", IndexAbout2.Text, SetingPath);
            SetConfig("Index", "RegName", IndexReg.Text, SetingPath);
            SetConfig("Index", "LoginName",IndexLogin.Text, SetingPath);
            
            SetConfig("Reg", "Title", RegIETitle.Text, SetingPath);
            SetConfig("Reg", "PageTitle",RegTitle.Text, SetingPath);
            SetConfig("Reg", "About1",  RegAbout1.Text, SetingPath);
            SetConfig("Reg", "About2", RegAbout2.Text, SetingPath);
            SetConfig("Reg", "MoNumberName", RegNumberName.Text, SetingPath);
            SetConfig("Reg", "MoNameName",RegNameName.Text, SetingPath);
            SetConfig("Reg", "MoPasswordName", RegPWName.Text, SetingPath);

            SetConfig("RegPost", "Title", RegPIETitle.Text, SetingPath);
            SetConfig("RegPost", "PageTitle", RegPTitle.Text, SetingPath);
            SetConfig("RegPost", "About1", RegPAbout1.Text, SetingPath);
            SetConfig("RegPost", "About2",  RegPAbout2.Text, SetingPath);
            SetConfig("RegPost", "TipsNone", RegPNone.Text, SetingPath);
            SetConfig("RegPost", "TipsExists", RegPExists.Text, SetingPath);
            SetConfig("RegPost", "TipsSuccess",  RegPSucc.Text , SetingPath);
            SetConfig("RegPost", "TipsNo", RegPNo.Text, SetingPath);
            
            SetConfig("Login", "Title", LoginIETtile.Text, SetingPath);
            SetConfig("Login", "PageTitle", LoginTitle.Text, SetingPath);
            SetConfig("Login", "About1",LoginAbout1.Text, SetingPath);
            SetConfig("Login", "About2", LoginAbout2.Text, SetingPath);

            SetConfig("LoginPost", "Title", LoginPIETitle.Text, SetingPath);
            SetConfig("LoginPost", "PageTitle",  LoginPTitle.Text, SetingPath);
            SetConfig("LoginPost", "About1",LoginPAbout1.Text, SetingPath);
            SetConfig("LoginPost", "About2", LoginPAbout2.Text, SetingPath);
            SetConfig("LoginPost", "TipsNone", LoginPNone.Text, SetingPath);
            SetConfig("LoginPost", "TipsNoExists", LoginPNoExists.Text, SetingPath);
            SetConfig("LoginPost", "TipsErrPW", LoginPErrPW.Text, SetingPath);

            SetConfig("MainPage", "Title", MPageIETitle.Text , SetingPath);
            SetConfig("MainPage", "PageTitle", MPageTitle.Text , SetingPath);
            SetConfig("MainPage", "About1",MPageAbout1.Text, SetingPath);
            SetConfig("MainPage", "About2",MPageAbout2.Text, SetingPath);

            SetConfig("ChangePage", "Title",CPageIETitle.Text, SetingPath);
            SetConfig("ChangePage", "PageTitle", CPageTitle.Text, SetingPath);
            SetConfig("ChangePage", "About1",CPageAbout1.Text, SetingPath);
            SetConfig("ChangePage", "About2", CPageAbout2.Text, SetingPath);
            SetConfig("ChangePage", "TipsNo",CPageNo.Text, SetingPath);
            SetConfig("ChangePage", "TipsNoNo",CPageNoNo.Text, SetingPath);
            SetConfig("ChangePage", "TipsSuccess", CPageSucc.Text, SetingPath);

            SetConfig("AdminPage", "Title", APageIETitle.Text, SetingPath);
            SetConfig("AdminPage", "PageTitle", APageTitle.Text, SetingPath);
            SetConfig("AdminPage", "About1", APageAbout1.Text , SetingPath);
            SetConfig("AdminPage", "About2",APageAbout2.Text, SetingPath);
            SetConfig("Admin", "Path", APagePath.Text, SetingPath);

            SetConfig("AdminPost", "Title", APPageIETitle.Text, SetingPath);
            SetConfig("AdminPost", "PageTitle",APPageTitle.Text, SetingPath);
            SetConfig("AdminPost", "About1", APPageAbout1.Text, SetingPath);
            SetConfig("AdminPost", "About2",  APPageAbout2.Text, SetingPath);
            SetConfig("AdminPost", "ListName1", APPageListName1.Text, SetingPath);
            SetConfig("AdminPost", "ListName2", APPageListName2.Text , SetingPath);

            SetConfig("Admin", "Name", AdminName.Text, SetingPath);
            SetConfig("Admin","Password",StrToMD5(AdminPW.Text),SetingPath);

            SetConfig("Main",null, null, SetingPath);
            SetConfig("Main", "paid", "ins|0", SetingPath);
            SetConfig("Main", "paname", "ins|1", SetingPath);
            for (int Index = 0; Index < listView1.Items.Count; Index++) {
                string GetConf = listView1.Items[Index].Text;
                if (GetConf == "文本") {
                    SetConfig("Main", "pa" + Index.ToString(), "str|"+listView1.Items[Index].SubItems[1].Text, SetingPath);
                }
                else if (GetConf == "数字")
                {
                    SetConfig("Main", "pa" + Index.ToString(), "int|" + listView1.Items[Index].SubItems[1].Text, SetingPath);
                }
            }
        }

        public static string StrToMD5(string str){
            byte[] data = Encoding.GetEncoding("GB2312").GetBytes(str);
             MD5 md5 = new MD5CryptoServiceProvider();
            byte[] OutBytes = md5.ComputeHash(data);

             string OutString = "";
             for (int i = 0; i < OutBytes.Length; i++)
            {
                 OutString += OutBytes[i].ToString("x2");
            }
            return OutString.ToLower();
         }
       

        private void button1_Click(object sender, EventArgs e)
        {
            ReadConfig();
            MessageBox.Show("载入配置完毕。");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WriteConfig();
            MessageBox.Show("应用配置完毕。");
        }

        protected string GetLocalIP()   //获取本地IP
        {
            IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];//获得当前IP地址
            return ipAddr.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string GropTitle = GropName.Text;
            NetDomain.Text = LocalIP.Text;
            NetPort.Text = "80";
            AdminName.Text = OneAdminName.Text;
            AdminPW.Text = OneAdminPW.Text;

            IndexIETitle.Text = GropTitle + "在线报名系统";
            MPageIETitle.Text = GropTitle + "在线报名系统";
            CPageIETitle.Text = "修改-" + GropTitle + "在线报名系统";
            RegIETitle.Text = "注册-"+GropTitle + "在线报名系统";
            RegPIETitle.Text = "注册-" + GropTitle + "在线报名系统";
            LoginIETtile.Text = "登录-" + GropTitle + "在线报名系统";
            LoginPIETitle.Text = "登录-" + GropTitle + "在线报名系统";
            APageIETitle.Text = "管理员登录-" + GropTitle + "在线报名系统";
            APPageIETitle.Text = "信息-" + GropTitle + "在线报名系统";

            IndexTitle.Text = GropTitle + "在线报名系统";
            RegTitle.Text = GropTitle + "在线报名系统";
            RegPTitle.Text = GropTitle + "在线报名系统";
            LoginTitle.Text = GropTitle + "在线报名系统";
            LoginPTitle.Text = GropTitle + "在线报名系统";
            MPageTitle.Text = GropTitle + "在线报名系统";
            CPageTitle.Text = GropTitle + "在线报名系统";
            APageTitle.Text = GropTitle + "在线报名系统";
            APPageTitle.Text = GropTitle + "在线报名系统";

            IndexAbout1.Text = GropTitle;
            RegAbout1.Text = GropTitle;
            RegPAbout1.Text = GropTitle;
            LoginAbout1.Text = GropTitle;
            LoginPAbout1.Text = GropTitle;
            MPageAbout1.Text = GropTitle;
            CPageAbout1.Text = GropTitle;
            APageAbout1.Text = GropTitle;
            APPageAbout1.Text = GropTitle;

            IndexAbout2.Text = "@"+GropAge.Text;
            RegAbout2.Text = "@" + GropAge.Text;
            RegPAbout2.Text = "@" + GropAge.Text;
            LoginAbout2.Text = "@" + GropAge.Text;
            LoginPAbout2.Text = "@" + GropAge.Text;
            MPageAbout2.Text = "@" + GropAge.Text;
            CPageAbout2.Text = "@" + GropAge.Text;
            APageAbout2.Text = "@" + GropAge.Text;
            APPageAbout2.Text = "@" + GropAge.Text;

            IndexTips.Text = "欢迎使用" + GropTitle + "在线报名系统";
            IndexReg.Text = "点击注册";
            IndexLogin.Text = "点击登录";

            RegNumberName.Text = "队长学号";
            RegNameName.Text = "队长姓名";
            RegPWName.Text = "登录密码";

            RegPSucc.Text = "注册成功！";
            RegPNone.Text = "信息为空！";
            RegPExists.Text = "学号已被注册！";
            RegPNo.Text = "存在非法字符！";

            LoginPNone.Text = "信息不能为空！";
            LoginPNoExists.Text = RegNumberName.Text+"不存在！";
            LoginPErrPW.Text = "密码错误！";

            CPageNo.Text = "非法访问！";
            CPageNoNo.Text = "存在非法符号！";
            CPageSucc.Text = "修改成功！";

            APagePath.Text = "admin";

            APPageListName1.Text = "主要负责人信息";
            APPageListName2.Text = "队伍总信息";

            MessageBox.Show("一键配置完毕，请点击[应用现在配置]进行应用。");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (AddName.Text != "" && comboBox1.Text != "")
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = comboBox1.Text;
                lvi.SubItems.Add(AddName.Text);
                listView1.Items.Add(lvi);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int Index = listView1.SelectedIndices[0];
            string Text = listView1.Items[Index].SubItems[1].Text;
            listView1.Items.RemoveAt(Index);
            MessageBox.Show("已删除项[" + Text + "],请点击[应用现在配置]进行应用。");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf(@"\")) + "/Python27/python.exe","ProjectSU.py");
        }

    }
}
