using System;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;

namespace WebServer
{
    public class APIController : BaseController
    {
        [HttpGet]
        public HttpResponseMessage Getdata()
        {
            var getParams = Request.GetQueryNameValuePairs();// GetParameters(data);

            var sql = string.Empty;
            var p_sql = getParams.Where(t => t.Key == "sql").FirstOrDefault();
            if (p_sql.Key.isEmpty() == false) sql = p_sql.Value;
            else
            {
                var p_table = getParams.Where(t => t.Key == "table").FirstOrDefault();
                var p_gcode = getParams.Where(t => t.Key == "gcode").FirstOrDefault();
                var p_where = getParams.Where(t => t.Key == "w").FirstOrDefault();
                var p_order = getParams.Where(t => t.Key == "o").FirstOrDefault();
                sql = "select * from {0} where gcode = '{gcode}'";
                sql = string.Format(sql, p_table.Value, p_gcode.Value);
                if (p_where.Key != null) sql += " and " + p_where.Value;
                if (p_order.Key != null) sql += " order by " + p_order.Value;
            }


            if (FCOMMON.info.Login.gcode == null)
                FCOMMON.info.Login.gcode = "EET1P";
            sql = sql.Replace("{gcode}", FCOMMON.info.Login.gcode);
            Console.WriteLine($"Getdata sql={sql}");
            var cs = "Data Source=10.131.15.18;Initial Catalog=EE;Persist Security Info=True;User ID=eeuser;Password=Amkor123!";
            var cn = new System.Data.SqlClient.SqlConnection(cs);
            var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
            var da = new System.Data.SqlClient.SqlDataAdapter(cmd);
            var dt = new System.Data.DataTable();
            da.Fill(dt);
            da.Dispose();
            cmd.Dispose();
            cn.Dispose();

            var txtjson = JsonConvert.SerializeObject(dt, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
            txtjson,
           System.Text.Encoding.UTF8,
           "application/json")
            };

            return resp;


        }

        [HttpGet]
        public HttpResponseMessage Gettable()
        {
            var getParams = Request.GetQueryNameValuePairs();// GetParameters(data);

            var sql = string.Empty;
            var p_sql = getParams.Where(t => t.Key == "sql").FirstOrDefault();
            if (p_sql.Key.isEmpty() == false) sql = p_sql.Value;
            else
            {
                var p_table = getParams.Where(t => t.Key == "table").FirstOrDefault();
                var p_gcode = getParams.Where(t => t.Key == "gcode").FirstOrDefault();
                var p_where = getParams.Where(t => t.Key == "w").FirstOrDefault();
                var p_order = getParams.Where(t => t.Key == "o").FirstOrDefault();
                sql = "select * from {0} where gcode = '{gcode}'";                
                sql = string.Format(sql, p_table.Value, p_gcode.Value);
                if (p_where.Key != null) sql += " and " + p_where.Value;
                if (p_order.Key != null) sql += " order by " + p_order.Value;
            }


            if (FCOMMON.info.Login.gcode == null)
                FCOMMON.info.Login.gcode = "EET1P";
            sql = sql.Replace("{gcode}", FCOMMON.info.Login.gcode);

            Console.WriteLine($"gettable sql={sql}");
            var cs = "Data Source=10.131.15.18;Initial Catalog=EE;Persist Security Info=True;User ID=eeuser;Password=Amkor123!";
            var cn = new System.Data.SqlClient.SqlConnection(cs);
            var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
            var da = new System.Data.SqlClient.SqlDataAdapter(cmd);
            var dt = new System.Data.DataTable();
            da.Fill(dt);
            da.Dispose();
            cmd.Dispose();
            cn.Dispose();

            var txtjson = JsonConvert.SerializeObject(dt, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
            txtjson,
           System.Text.Encoding.UTF8,
           "application/json")
            };

            return resp;


        }



        [HttpGet]
        public HttpResponseMessage Index()
        {
            //로그인이 되어있지않다면 로그인을 가져온다
            MethodResult result;
            result = View();

            var model = GetGlobalModel();
            var getParams = Request.GetQueryNameValuePairs();// GetParameters(data);

            //기본값을 찾아서 없애줘야한다
            var contents = result.Content;

            //공용값 적용
            ApplyCommonValue(ref contents);

            //최종문자 적용
            result.Content = contents;

            var resp = new HttpResponseMessage()
            {
                Content = new StringContent(
              result.Content,
              System.Text.Encoding.UTF8,
              "text/html")
            };

            return resp;
        }

    }
}
