using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using System.Net.Http;
using Newtonsoft.Json;
using System.Configuration;
using System.Text;

namespace AMESCoreStudio.Web.Controllers
{
    //[ApiController]
    public class APIController : ControllerBase
    {
        private static HttpClient NewClient()
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.Timeout = TimeSpan.FromHours(1);//1小時,可自訂義
            //client.DefaultRequestHeaders.Add("x-api-key", System.Configuration.ConfigurationManager.AppSettings["x-api-key"]);
            return client;
        }

        private static string ApiServiceIP = AppSetting.Setting.ApiUrl;

        [HttpGet]
        public string GetMethod(string apiUrl)
        {
            ///sResult = GET_API("http://10.168.245.111:5000/" ,"api/WipInfos/WipInfoByWipNo/sWO");

            var client = NewClient();
            try
            {
                apiUrl = ApiServiceIP + apiUrl;
                var aa = client.GetAsync(apiUrl);
                HttpResponseMessage response = client.GetAsync(apiUrl).Result;

                if (response.IsSuccessStatusCode)
                {
                    var jsonString = response.Content.ReadAsStringAsync();
                    jsonString.Wait();
                    //List<TResult> data = JsonConvert.DeserializeObject<List<TResult>>(jsonString.Result);
                    return jsonString.Result;
                }
                return "error";
            }
            catch
            {
                //Console.WriteLine(ex.Message);
                return "無法連線WebAPI";
            }
        }

        [HttpPost]
        public string PostMethod(string apiUrl, string postdata)
        {
            var client = NewClient();

            try
            {
                apiUrl = ApiServiceIP + apiUrl;
                HttpResponseMessage response = client.PostAsync(apiUrl,
                    new StringContent(postdata,
                    Encoding.UTF8, "application/json")).Result;

                if (response.IsSuccessStatusCode)
                {
                    var jsonString = response.Content.ReadAsStringAsync();
                    jsonString.Wait();
                    return jsonString.Result;
                }
                //可能會發生錯誤            
                return "error";

            }
            catch
            {
                return "無法連線WebAPI";
            }
        }

        public static bool PostInsertMethod(string apiUrl, string postdata)
        {
            bool bInsertSuccess = false;//是否新增資料成功
            var client = NewClient();

            try
            {
                apiUrl = ApiServiceIP + apiUrl;
                HttpResponseMessage response = client.PostAsync(apiUrl,
                    new StringContent(postdata,
                    Encoding.UTF8, "application/json")).Result;

                if (response.IsSuccessStatusCode)
                {
                    bInsertSuccess = true;
                }
                //可能會發生錯誤            
                return bInsertSuccess;
            }
            catch
            {
                return bInsertSuccess;
            }
        }

        [HttpPut]
        public string PutMethod(string apiUrl, string postdata)
        {
            bool bUpdateSuccess = false;//是否更新資料成功
            var client = NewClient();

            try
            {
                apiUrl = ApiServiceIP + apiUrl;
                HttpResponseMessage response = client.PutAsync(apiUrl,
                    new StringContent(postdata,
                    Encoding.UTF8, "application/json")).Result;

                if (response.IsSuccessStatusCode)
                {
                    bUpdateSuccess = true;

                    var jsonString = response.Content.ReadAsStringAsync();
                    jsonString.Wait();
                    return jsonString.Result;
                }
                //可能會發生錯誤            
                return "error";
            }
            catch
            {
                return "無法連線WebAPI";
            }
        }

        [HttpDelete]
        public static bool DeteleMethod(string apiUrl)
        {
            bool bDeleteSuccess = false;//是否刪除資料成功
            var client = NewClient();

            try
            {
                apiUrl = ApiServiceIP + apiUrl;
                HttpResponseMessage response = client.DeleteAsync(apiUrl).Result;

                if (response.IsSuccessStatusCode)
                {
                    bDeleteSuccess = true;
                }
                //可能會發生錯誤            
                return bDeleteSuccess;
            }
            catch
            {
                return bDeleteSuccess;
            }
        }
    }
}