using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemX.Net;
using SystemX.Net.BaseProtocol;

namespace SystemX.Common.Protocol
{
    using ParamElement = Tuple<SqlDbType, int, byte, byte>;

    public static class CommonParamSet
    {
        public static Dictionary<string, ParamElement> fParam= new Dictionary<string, ParamElement>();

        public static void TestResultField()
        {
            fParam.Clear();
            fParam = new Dictionary<string, ParamElement>();
            fParam.Add("No", new ParamElement(SqlDbType.BigInt, 0, 0, 0));
            fParam.Add("TestDT", new ParamElement(SqlDbType.DateTime2, 7, 0, 0));
            fParam.Add("LogData", new ParamElement(SqlDbType.NVarChar, -1, 0, 0));
        }


        public static SqlParameter GetMakeSqlParameterInfo(Dictionary<string, ParamElement> refField, string strSetName, object objValue)
        {
            if (refField.ContainsKey(strSetName))
            {
                SqlParameter param = null;

                if (refField[strSetName].Item2 != 0)
                    param = new SqlParameter("@" + strSetName, refField[strSetName].Item1, refField[strSetName].Item2);
                else
                    param = new SqlParameter("@" + strSetName, refField[strSetName].Item1);

                if (refField[strSetName].Item1 == SqlDbType.Decimal)
                {
                    param.Precision = refField[strSetName].Item3;
                    param.Scale = refField[strSetName].Item4;
                }

                param.Value = objValue;
                return param;
            }
            else
                return null;
        }
    }

    public class CommonCustomProtocol : BASE_PROTOCOL
    {
        public CommonCustomProtocol() : base()
        {
        }

        public CommonCustomProtocol(ushort usGetCommnad, ushort usGetSubCommand) : base(usGetCommnad, usGetSubCommand)
        {
        }

        public CommonCustomProtocol(PROTOCOL_CODE SET_CODE) : base(SET_CODE)
        {
        }

        public SqlCommand LogDataResultInsert(string strTableName, DataRow itemData)
        {
            string columns = string.Empty;
            columns = "No,TestDT,DataLog";

            string values = string.Join(",", columns.Split(',').Select(c => string.Format("@{0}", c)));
            string sqlCommand = string.Format("INSERT INTO [" + strTableName + "] ({0}) VALUES ({1})", columns, values);

            CommonParamSet.TestResultField();

            SqlCommand cmd = new SqlCommand(sqlCommand);

            SqlParameter[] setParams = new SqlParameter[columns.Split(',').Count()];

            setParams[0] = CommonParamSet.GetMakeSqlParameterInfo(CommonParamSet.fParam, "No", itemData["No"]);
            setParams[1] = CommonParamSet.GetMakeSqlParameterInfo(CommonParamSet.fParam, "TestDT", itemData["TestDT"]);
            setParams[2] = CommonParamSet.GetMakeSqlParameterInfo(CommonParamSet.fParam, "LogData", itemData["LogData"]);

            cmd.Parameters.AddRange(setParams);

            return cmd;
        }
    }
}
