using Forks.JsonRpc.Client;
using Forks.JsonRpc.Client.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace BO.Utils
{
  public class LoginRpcUtil
  {
    public static string GetUserNameByCode(string code, out string error)
    {
      try
      {
        error = string.Empty;
        const string wpfUserMethod = "/MainSystem/B3ClientService/Rpcs/UserInfoRpc/GetUserName";
        return RpcFacade.Call<string>(wpfUserMethod, code);
      }
      catch (Exception ex)
      {
        error = ex.ToString();
      }
      return string.Empty;
    }

    public static void FillUserEmpInfo(string name, LoginUserInfo userInfo)
    {
      const string wpfUserMethod = "/MainSystem/B3ClientService/Rpcs/UserInfoRpc/GetUserEmpInfo";
      var obj = RpcFacade.Call<RpcObject>(wpfUserMethod, name);
      if (obj != null)
      {
        userInfo.ID = obj.Get<long>("User_ID");
        userInfo.UserName = obj.Get<string>("User_Name");
        userInfo.Domain_ID = obj.Get<long>("Domain_ID");
        userInfo.AccountingUnit_ID = obj.Get<long?>("AccountingUnit_ID");
        userInfo.AccountingUnit_Name = obj.Get<string>("AccountingUnit_Name");
        userInfo.Department_ID = obj.Get<long?>("Department_ID");
        userInfo.Department_Name = obj.Get<string>("Department_Name");
        userInfo.Employee_ID = obj.Get<long>("Employee_ID");
        userInfo.Employee_Name = obj.Get<string>("Employee_Name");
        userInfo.Role = obj.Get<string>("Role");
      }
    }

    public static bool TestConnection(int? millisecondsTimeout=null)
    {
      var url = ButcherAppContext.Context.UrlConfig.ServerUrl;
      if (string.IsNullOrEmpty(url))
        return false;
      //try
      //{
        //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
      var uri = new Uri(url);
      if (millisecondsTimeout == null)
      {
        millisecondsTimeout = 50;
      }
      return TestConnection(uri.Host, uri.Port, millisecondsTimeout.Value);

      //  HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

      //  return resp.StatusCode == HttpStatusCode.OK;
      //}
      //catch
      //{
      //  return false;
      //}
    }

    //public static bool TestConnection()
    //{
    //  try
    //  {
    //    Dns.GetHostEntry(ButcherAppContext.Context.UrlConfig.ServerUrl); //using System.Net;
    //    return true;
    //  }
    //  catch (System.Net.Sockets.SocketException ex)
    //  {
    //    return false;
    //  }
    //  //try
    //  //{
    //  //  Ping objPingSender = new Ping();
    //  //  PingOptions objPinOptions = new PingOptions();
    //  //  objPinOptions.DontFragment = true;
    //  //  string data = "";
    //  //  byte[] buffer = Encoding.UTF8.GetBytes(data);
    //  //  int intTimeout = 120;
    //  //  PingReply objPinReply = objPingSender.Send(ButcherAppContext.Context.UrlConfig.ServerUrl, intTimeout, buffer, objPinOptions);
    //  //  string strInfo = objPinReply.Status.ToString();
    //  //  if (strInfo == "Success")
    //  //  {
    //  //    return true;
    //  //  }
    //  //  else
    //  //  {
    //  //    return false;
    //  //  }
    //  //}
    //  //catch (Exception)
    //  //{
    //  //  return false;
    //  //}
    //}


    public static bool TestConnection(string host, int port, int millisecondsTimeout)
    {
      var client = new System.Net.Sockets.TcpClient();
      try
      {
        var ar = client.BeginConnect(host, port, null, null);
        ar.AsyncWaitHandle.WaitOne(millisecondsTimeout);
        return client.Connected;
      }
      catch (Exception)
      {
        return false;
      }
      finally
      {
        client.Close();
      }
    }
  }
}
