using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace XYY.Common.Standard
{
    public static class DataXmlUpdateHelper
    {

        public static string Xml<T>(IEnumerable<T> list)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(IEnumerable<T>));
            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
            serializer.Serialize(writer, list);
            string data = writer.ToString();
            writer.Close();
            writer.Dispose();
            return data;
        }

        public static string GetColumns<T>(T t)
        {
            var type = t.GetType();
            var pars = type.GetProperties();
            List<string> columnTypes = new List<string>();
            string columns = "";
            foreach (var par in pars)
            {
                //if (par.Name.ToUpper() != "ID")
                //{

                string columnType = "";

                if (par.PropertyType == typeof(string))
                {
                    columnType = "nvarchar(512)";
                }
                else if (par.PropertyType == typeof(DateTime?))
                {
                    columnType = "datetime ";
                }
                else if (par.PropertyType == typeof(DateTime))
                {
                    columnType = "datetime";
                }
                else if (par.PropertyType == typeof(int?))
                {
                    columnType = "int";
                }
                else if (par.PropertyType == typeof(int))
                {
                    columnType = "int";
                }
                else if (par.PropertyType == typeof(bool))
                {
                    columnType = "bit";
                }
                else { columnType = "nvarchar(555)"; }
                columnTypes.Add("[" + par.Name + "] " + columnType);
                //}
            }
            return string.Join(',', columnTypes);
        }

        public static string Sets<T>(T t)
        {

            var type = t.GetType();
            var pars = type.GetProperties();
            List<string> columnTypes = new List<string>();
            string columns = "";
            foreach (var par in pars)
            {
                if (par.Name.ToUpper() != "ID")
                {
                    columns = $"a.{par.Name}=b.{par.Name}";
                    columnTypes.Add(columns);
                }
            }
            return string.Join(",", columnTypes);
        }

        public static string XmlValues<T>(T t)
        {
            var type = t.GetType();
            var pars = type.GetProperties();
            List<string> columnTypes = new List<string>();
            foreach (var par in pars)
            {
                string columns = "";
                if (par.GetValue(t, null) != null)
                {
                    if (par.PropertyType == typeof(int) || par.PropertyType == typeof(int?) || par.PropertyType == typeof(string))
                        columns = $"<{par.Name}>{par.GetValue(t).ToString().Replace("'", "''")}</{par.Name}>";
                    else if (par.PropertyType == typeof(DateTime))
                    {
                        var val = (DateTime)par.GetValue(t);
                        if (val != DateTime.MinValue)
                            columns = $"<{par.Name}>{val.ToString("yyyy-MM-dd HH:mm:ss")}</{par.Name}>";
                    }
                    else if (par.PropertyType == typeof(DateTime?))
                    {
                        var val = (DateTime?)par.GetValue(t);
                        if (val != DateTime.MinValue)
                            columns = $"<{par.Name}>{val.Value.ToString("yyyy-MM-dd HH:mm:ss")}</{par.Name}>";
                    }
                    else if (par.PropertyType == typeof(bool))
                    {
                        var val = (bool)par.GetValue(t);
                        columns = $"<{par.Name}>{(val ? "1" : "0")}</{par.Name}>";
                    }

                }
                else
                {
                    //columns = $"<{par.Name}></{par.Name}>";
                }
                columnTypes.Add(columns);
            }
            return $"<item>{ string.Join("\r\n", columnTypes)}</item>";
        }


    }
}
