using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Xml;
using WHC.OrderWater.Commons;
using MOKA_Factory_Tools;

public class Xmlconfig
{
	public static bool ReadXmlnode(string str,string node,string value,out object result)
	{
        result = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            //xml.Load(path);
            xml.LoadXml(str);
            var b = xml.SelectSingleNode(node);
            result = xml.SelectSingleNode(node).SelectSingleNode(value).InnerText;
            return true;
        }
        catch(Exception ex)
        {
            result = ex.Message;
            return false;
        }
        
    }

    public static bool WriteXmlnode(string path, string node, string value, out object result)
    {
        result = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            xml.SelectSingleNode(node).InnerText = value;
            return true;
        }
        catch (Exception ex)
        {
            result = ex.Message;
            return false;
        }
    }

    public static bool KeyCountXml(string str, string node, string node2,out string code,out string desc,out string value)
    {
        code = "";
        value = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            //xml.Load(path);
            xml.LoadXml(str);
            code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;          
            desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
            if(code=="200")
            {
                value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("value").InnerText;
                return true;
            }
            return false;

        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }

    }

    public static bool AutoKeyStatusXml(string str, string node, out string code, out string desc)
    {
        code = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            //xml.Load(path);
            xml.LoadXml(str);
            code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
            desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
            if (code == "200")
            {
                return true;
            }
            return false;

        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }

    }

    public static bool GetDIDKeyXml(string str, string node, string node2, out string code, out string desc, out string value)
    {
        code = "";
        value = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            //xml.Load(path);
            xml.LoadXml(str);
            code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
            desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
            if (code == "200")
            {
                value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("deviceid").InnerText;
                string CRC32= xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("crc32").InnerText; 
                if (CrcUtils.CRC32(value) ==Convert.ToUInt32(CRC32, 16))
                {
                    return true;
                }
                else
                {
                    desc = "CRC32 error";
                    return false;                 
                }
            }
            return false;
        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }
    }

    public static bool GetOthersKeyXml(string str, string node, string node2, out string code, out string desc, out string value, out string md5)
    {
        md5 = "";
        code = "";
        value = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            //xml.Load(path);
            xml.LoadXml(str);
            code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
            desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
            if (code == "200")
            {
                value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("value").InnerText;
                if (xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("md5value") != null)
                    md5 = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("md5value").InnerText;
                string CRC32 = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("crc32").InnerText;
                if (CrcUtils.CRC32(value) == Convert.ToUInt32(CRC32, 16))
                {
                    return true;
                }
                else
                {
                    desc = "CRC32 error";
                    return false;
                }
            }
            return false;
        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }
    }

    /// <summary>
    /// 解析并获取返回2个key的xml.
    /// </summary>
    /// <returns></returns>
    public static bool Get2KeyInfoFromXml(string str, string dataKey, string toolKey, out string code, out string desc, out string acaskey_data, out string acaskey_datamd5, out string acaskey_tool, out string acaskey_toolmd5)
    {
        acaskey_data = "";
        acaskey_tool = "";
        code = "";
        acaskey_datamd5 = "";
        acaskey_toolmd5 = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(str);
            code = xml.SelectSingleNode("response").Attributes.GetNamedItem("code").InnerText;
            desc = xml.SelectSingleNode("response").Attributes.GetNamedItem("desc").InnerText;
            if (code == "200")
            {
                acaskey_data = xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("value").InnerText;
                if (xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("md5value") != null)
                    acaskey_datamd5 = xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("md5value").InnerText;

                bool bCRC321 = false;
                string CRC321 = "";
                if (xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("crc32") != null)
                {
                    bCRC321 = true;
                    CRC321 = xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("crc32").InnerText;
                }

                acaskey_tool = xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("value").InnerText;
                if (xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("md5value") != null)
                    acaskey_toolmd5 = xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("md5value").InnerText;

                bool bCRC322 = false;
                string CRC322 = "";
                if (xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("crc32") != null)
                {
                    bCRC322 = true;
                    CRC322 = xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("crc32").InnerText;
                }

                if (bCRC321)
                { 
                    if (CrcUtils.CRC32(acaskey_data) != Convert.ToUInt32(CRC321, 16))
                    {
                        desc = "CRC32 error";
                        return false;
                    } 
                }

                if (bCRC322)
                {
                    if (CrcUtils.CRC32(acaskey_tool) != Convert.ToUInt32(CRC322, 16))
                    {
                        desc = "CRC32 error";
                        return false;
                    }
                }
            }

            return true;
        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }
    }

    public static bool GetWidevineAndAttestationKeyXml(string str, 
        string node, 
        out string code, 
        out string desc, 
        out bool isAndroidTV,
        out string WidevineKey,
        out string AttestationKey,
        out string AttestationMd5)
    {
        code = "";
        isAndroidTV = false;
        WidevineKey = "";
        AttestationKey = "";
        AttestationMd5 = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            //xml.Load(path);
            xml.LoadXml(str);
            code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
            desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
            isAndroidTV = !xml.SelectSingleNode(node).Attributes.GetNamedItem("androidtv").InnerText.Equals("1");
            if (code == "200")
            {
                WidevineKey = xml.SelectSingleNode(node + "//widevine").Attributes.GetNamedItem("value").InnerText;
                string CRC32 = xml.SelectSingleNode(node + "//widevine").Attributes.GetNamedItem("crc32").InnerText;
                if (CrcUtils.CRC32(WidevineKey) != Convert.ToUInt32(CRC32, 16))
                {
                    desc = "widevine CRC32 error";
                    return false;
                }

                if (isAndroidTV)
                {
#if false
                    // 判断节点是否存在;
                    if (xml.GetElementsByTagName("attestation") == null)
                    {
                        desc = "isAndroidTV: but attestation node is Null";
                        return false;
                    }
#endif
                    AttestationKey = xml.SelectSingleNode(node + "//attestation").Attributes.GetNamedItem("value").InnerText;
                    CRC32 = xml.SelectSingleNode(node + "//attestation").Attributes.GetNamedItem("crc32").InnerText;
                    if (CrcUtils.CRC32(AttestationKey) != Convert.ToUInt32(CRC32, 16))
                    {
                        desc = "attestation CRC32 error";
                        return false;
                    }
                    else
                    {
                        AttestationMd5 = xml.SelectSingleNode(node + "//attestation").Attributes.GetNamedItem("md5value").InnerText;
                    }
                }

                return true;
            }

            return false;
        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }
    }

    public static bool ReportXml(string str, string node, out string code, out string desc)
    {
        code = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            //xml.Load(path);
            xml.LoadXml(str);
            code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
            desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
            if (code == "200")
            {
                return true;
            }
            return false;

        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }

    }

    public static bool GetDSNXml(string str, string node, out string code, out string desc, out string value)
    {
        code = "";
        value = "";
        try
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(str);
            code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
            desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
            if (code == "200")
            {
                value = xml.SelectSingleNode(node + "//" + "firetvDsn").Attributes.GetNamedItem("dsn").InnerText;
                return true;
            }
            return false;
        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }
    }

    #region G客户NTF统计功能
    public static bool GetMaInfoXml(string str, out MaInfo maInfo, out string desc)
    {
        desc = "";
        maInfo = null;

        try
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(str);
            XmlNamespaceManager nsp = new XmlNamespaceManager(doc.NameTable);
            nsp.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
            XmlNode node = doc.SelectSingleNode("//soap:Envelope//soap:Body", nsp);
            if ( node != null)
            {
                JObject jObject = JObject.Parse(node.InnerText);
                if ( jObject["IsSuccess"].Value<bool>() )
                {
                    maInfo = JsonConvert.DeserializeObject<MaInfo>(jObject["Msg"].ToString());
                }
                else
                {
                    return false;
                }
            }           
        }
        catch (Exception ex)
        {
            desc = ex.Message;
            return false;
        }

        return true;
    }
    #endregion
}



