using UnityEngine;
using System;
using System.Xml;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Collections;
using SmartX1Demo;

public class SmartController : MonoBehaviour 
{
    int[] keyHandles = new int[8];
    int[] keyNumber = new int[8];
    private int Rtn = 0;
    SmartApp smart = new SmartApp();
    private bool IsQuit = false;
    private DateTime t1, t2, t11;
    string appid = "AXKC";

    // Use this for initialization
    void Start () {
        t2 = DateTime.Now;
        t1 = DateTime.Now;
        DontDestroyOnLoad(GameObject.Find("SmartX1"));
        tipStyle = new GUIStyle();
        tipStyle.fontSize = 40;
        tipStyle.normal.textColor = Color.red;

        original_filepath = Application.streamingAssetsPath + @"/xml/SmartXProject.xml";
        encode_filepath = Application.streamingAssetsPath + @"/xml/enSmartXProject.xml";
        decode_filepath = Application.streamingAssetsPath + @"/xml/deSmartXProject.xml";

        ControlXML();

        //检测加密狗是否存在
        FindSmartX1();
	}
    
    public string GetAppID(string filepath)
    {
        if (File.Exists(filepath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlNode root = xmlDoc.SelectSingleNode("SmartX1");
            XmlNodeList nodeList = root.ChildNodes;
            foreach (XmlElement elem in nodeList)
            {
                if (elem.Name == "BaseInfo")
                {
                    foreach (XmlElement zq in elem.ChildNodes)
                    {
                        if (zq.Name == "AppID")
                        {
                            return zq.InnerText;
                        }
                    }
                }

            }
        }
        return "";
    }

    bool FindSmartX1()
    {
        try
        {
            Rtn = smart.SmartX1Find(appid, keyHandles, keyNumber);
            if (Rtn != 0)
            {
                IsQuit = true;
                t1 = DateTime.Now;
                t2 = DateTime.Now;
                t11 = DateTime.Now;
                return false;
            }
            Debug.Log("SmartX1Find Successfully");
            return true;
        }
        catch (Exception ex)
        {
            Debug.Log(keyHandles[0]);
            Debug.Log("异常" + ex.Message);
            GameObject.Find("Application").GetComponent<ApplicationClose>().isSmartX1Exit = true;
            Application.Quit();
            return false;
        }
    }

    bool CheckExist()
    {
        try
        {
            Rtn = smart.SmartX1CheckExist(keyHandles[0]);
            if (Rtn != 0)
            {
                //Debug.Log("SmartX1 not exist!" + Rtn.ToString());
                IsQuit = true;
                t1 = DateTime.Now;
                t2 = DateTime.Now;
                t11 = DateTime.Now;
                return false;
            }
            //Debug.Log("SmartX1Exist!");
            return true;
        }
        catch (Exception ex)
        {
            //Debug.Log(keyHandles[0]);
            //Debug.Log("异常" + ex.Message);
            GameObject.Find("Application").GetComponent<ApplicationClose>().isSmartX1Exit = true;
            Application.Quit();
            return false;
        }
    }

    private GUIStyle tipStyle;
    private int timer = 5;
    void OnGUI()
    {
        if (IsQuit)
        {
            t2 = DateTime.Now;
            if(t2-t11>new TimeSpan(0,0,1))
            {
                if(timer>0)
                    timer--;
                t11 = t2;
            }
            GUI.Label(new Rect(Screen.width / 2 - 150, Screen.height / 2 - 75, 300, 150), "密钥验证失败,程序将在"+timer+"秒后退出!", tipStyle);            
            
            if (t2 - t1 > new TimeSpan(0, 0, 5))
            {
                //Debug.Log("程序退出!");
                GameObject.Find("Application").GetComponent<ApplicationClose>().isSmartX1Exit = true;
                Application.Quit();
            }
        }
    }

	// Update is called once per frame
	void Update () {
        if (IsQuit)
            return;
        t2 = DateTime.Now;
        if (t2 - t1 > new TimeSpan(0, 0, 0, 10, 0))
        {
            t1 = t2;
            CheckExist();
        }
	}

    //xml文件加密解密
    void ControlXML()
    {
        if (File.Exists(original_filepath))//证明这次是第一次运行该程序,获取当前配置文件
        {
            appid = GetAppID(original_filepath);
            //StartCoroutine(GenerateEncodeXML());
            GenerateEncodeXML1();
        }
        else
        {
            if (File.Exists(encode_filepath))
            {
                fileDcryption(encode_filepath);
                if (File.Exists(decode_filepath))
                {
                    appid = GetAppID(decode_filepath);
                    File.Delete(decode_filepath);
                }
            }
        }
    }

    IEnumerator GenerateEncodeXML()
    {
        yield return new WaitForSeconds(1.0f);
        fileEncryption(original_filepath);
        if (File.Exists(encode_filepath))
        {
            File.Delete(original_filepath);
        }
    }

    void GenerateEncodeXML1()
    {
        fileEncryption(original_filepath);
        if (File.Exists(encode_filepath))
        {
            File.Delete(original_filepath);
        }
    }

    //加密xml文件
    private void Encrypt(XmlDocument doc, string ElementName, SymmetricAlgorithm key)
    {
        XmlElement elementEncrypt = doc.GetElementsByTagName(ElementName)[0] as XmlElement;
        EncryptedXml eXml = new EncryptedXml();
        byte[] encryptElement = eXml.EncryptData(elementEncrypt, key, false);//
        EncryptedData edElement = new EncryptedData();
        edElement.Type = EncryptedXml.XmlEncElementUrl;
        string encryptionMethod = null;

        if (key is TripleDES)
        {
            encryptionMethod = EncryptedXml.XmlEncTripleDESUrl;
        }
        else if (key is DES)
        {
            encryptionMethod = EncryptedXml.XmlEncDESUrl;
        }

        if (key is Rijndael)
        {
            switch (key.KeySize)
            {
                case 128:
                    encryptionMethod = EncryptedXml.XmlEncAES128Url;
                    break;
                case 192:
                    encryptionMethod = EncryptedXml.XmlEncAES192Url;
                    break;
                case 256:
                    encryptionMethod = EncryptedXml.XmlEncAES256Url;
                    break;
            }
        }
        edElement.EncryptionMethod = new EncryptionMethod(encryptionMethod);
        edElement.CipherData.CipherValue = encryptElement;
        EncryptedXml.ReplaceElement(elementEncrypt, edElement, false);
    }

    //XML文件解密
    private void Decrypt(XmlDocument doc, SymmetricAlgorithm Alg)
    {
        XmlElement encryptedElement = doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
        EncryptedData edElement = new EncryptedData();
        edElement.LoadXml(encryptedElement);
        EncryptedXml exml = new EncryptedXml();
        byte[] rgbOutput = exml.DecryptData(edElement, Alg);
        exml.ReplaceData(encryptedElement, rgbOutput);
    }

    string original_filepath = "";
    string encode_filepath = "";
    string decode_filepath = "";

    //举例,对某个XML文件加密
    private void fileEncryption(string filename)
    {
        RijndaelManaged key = new RijndaelManaged();
        //设置密钥:key为32位=数字或字母16个=汉子8个
        byte[] byteKey = Encoding.Unicode.GetBytes("1111111111111111");
        key.Key = byteKey;
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.PreserveWhitespace = true;
        xmldoc.Load(original_filepath);//想要加密的xml文件
        Encrypt(xmldoc, "SmartX1", key);//需要加密的节点
        if (key != null)
        {
            key.Clear();
        }
        xmldoc.Save(encode_filepath);//生成加密后的xml文件
    }

    //举例,对某个xml文件解密
    private void fileDcryption(string filename)
    {
        RijndaelManaged key = new RijndaelManaged();
        byte[] byteKey = Encoding.Unicode.GetBytes("1111111111111111");
        key.Key = byteKey;
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.PreserveWhitespace = true;
        xmldoc.Load(encode_filepath);//加载要解密的xml文件
        Decrypt(xmldoc, key);
        if (key != null)
        {
            key.Clear();
        }
        xmldoc.Save(decode_filepath);//生成解密后的文件
    }
}