//
 // GET: /Service/
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     if (filterContext.RouteData.Values["action"].ToString().ToLower() != ("DoLogin").ToLower() && Session["ApiCode"] == null)
     {
         OperationResult result = new OperationResult();
         result.ErrorCode = "00001";
         result.ErrorMessage = Message.GetMessage(MessageCode.Error00001, GetRequestCulture());
         
         filterContext.Result = Json(new
         {
             Result = result
         }, JsonRequestBehavior.AllowGet);
 
     }
          
 }
        public static OperationResult UpdateProduct(Product product)
        {
            //POST
            var client = GetClient();
            OperationResult operationResult = new OperationResult();

            using (HttpResponseMessage response = client.PostAsJsonAsync("Product/UpdateProduct", product).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    operationResult = response.Content.ReadAsAsync<OperationResult>().Result;
                }
                else
                {
                    operationResult.ErrorMessage = String.Format("Status Code: {0}, Reason Pharse: {1}",
                        response.StatusCode, response.ReasonPhrase);
                }
            }
            return operationResult;
        }
        public static OperationResult GetUserMessage(GetMessageCriteria getMessageCriteria)
        {
            //POST
            var client = GetClient();
            OperationResult operationResult = new OperationResult();

            using (HttpResponseMessage response = client.PostAsJsonAsync("Integration/GetUserMessages", getMessageCriteria).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    operationResult = response.Content.ReadAsAsync<OperationResult>().Result;
                    if (operationResult.Result)
                        operationResult.Response = JsonConvert.DeserializeObject<UserMessages>(operationResult.Response.ToString());
                }
                else
                {
                    operationResult.ErrorMessage = String.Format("Status Code: {0}, Reason Pharse: {1}",
                        response.StatusCode, response.ReasonPhrase);
                }
            }
            return operationResult;
        }
        public JsonResult DoLogin(string param)
        {
            OperationResult result = new OperationResult();
            try
            {
                Login loginModel = JsonConvert.DeserializeObject<Login>(param);
                result =
                    Utils.ServiceHelper.AuthenticationService.Dologin(loginModel);
                if (result.Result)
                    Session["ApiCode"] = (result.Response as User).ApiCode;
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;

            }


            return Json(new
            {
                Result = result
            }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetStores(string param)
        {
            OperationResult result = new OperationResult();
            try
            {
                GetStoreCriteria getStoreCriteria = JsonConvert.DeserializeObject<GetStoreCriteria>(param);
                if (Session["ApiCode"] != null)
                    getStoreCriteria.ApiCode = Session["ApiCode"].ToString();
                result =
                    Utils.ServiceHelper.IntegrationService.GetStore(getStoreCriteria);
                 
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;

            }


            return Json(new
            {
                Result = result
            }, JsonRequestBehavior.AllowGet);
        }
        public static OperationResult SetOrderShipmentInformation(SetShipmentCriteria setShipmentCriteria)
        {
            //POST
            var client = GetClient();
            OperationResult operationResult = new OperationResult();

            using (HttpResponseMessage response = client.PostAsJsonAsync("Integration/SetOrderShipmentInformation", setShipmentCriteria).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    operationResult = response.Content.ReadAsAsync<OperationResult>().Result;
                    if (operationResult.Result)
                        operationResult.Response = JsonConvert.DeserializeObject<List<IntegrationShipmentCompany>>(operationResult.Response.ToString());

                }
                else
                {
                    operationResult.ErrorMessage = String.Format("Status Code: {0}, Reason Pharse: {1}",
                        response.StatusCode, response.ReasonPhrase);
                }
            }
            return operationResult;
        }
        public static OperationResult ConfirmOrRejectOrderItem(ConfirmOrRejectOrderItemCriteria confirmOrRejectOrderItemCriteria)
        {
            //POST
            var client = GetClient();
            OperationResult operationResult = new OperationResult();

            using (HttpResponseMessage response = client.PostAsJsonAsync("Integration/ConfirmOrRejectOrderItem", confirmOrRejectOrderItemCriteria).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    operationResult = response.Content.ReadAsAsync<OperationResult>().Result; 
                }
                else
                {
                    operationResult.ErrorMessage = String.Format("Status Code: {0}, Reason Pharse: {1}",
                        response.StatusCode, response.ReasonPhrase);
                }
            }
            return operationResult;
        }
        public JsonResult UpdateProduct(string param)
        {
            OperationResult result = new OperationResult();
            try
            {
                Product product = JsonConvert.DeserializeObject<Product>(param);
                if (Session["ApiCode"] != null)
                    product.ApiCode = Session["ApiCode"].ToString();
                result =
                    Utils.ServiceHelper.ProductService.UpdateProduct(product);

            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;

            }


            return Json(new
            {
                Result = result
            }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetCurrencies()
        {
            OperationResult result = new OperationResult();
            try
            {
                GetCurrencyCriteria getCurrencyCriteria = new GetCurrencyCriteria();
                if (Session["ApiCode"] != null)
                    getCurrencyCriteria.ApiCode = Session["ApiCode"].ToString();
                result =
                    Utils.ServiceHelper.GeneralService.GetCurrencies(getCurrencyCriteria);

            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;

            }


            return Json(new
            {
                Result = result
            }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult ConfirmOrRejectOrderItem(string param)
        {
            OperationResult result = new OperationResult();
            try
            {
                ConfirmOrRejectOrderItemCriteria confirmOrRejectOrderItemCriteria = JsonConvert.DeserializeObject<ConfirmOrRejectOrderItemCriteria>(param);
                if (Session["ApiCode"] != null)
                    confirmOrRejectOrderItemCriteria.ApiCode = Session["ApiCode"].ToString();
                result =
                    Utils.ServiceHelper.IntegrationService.ConfirmOrRejectOrderItem(confirmOrRejectOrderItemCriteria);

            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;

            }


            return Json(new
            {
                Result = result
            }, JsonRequestBehavior.AllowGet);
        }