using BeachOrder.Models;
using ErsPos.Data.Model;
using ErsPos.Service.ViewModels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace BeachOrder.Helpers
{
    public static class Helpers
    {
        public static string ToExceptionString(this Exception ex)
        {
            return ex?.InnerException?.Message.ToString() ?? ex?.Message.ToString() ?? "Error";
        }
        public static string ToSafeString(this object value)
        {
            return value == null ? "" : value.ToString();
        }
        public static bool HasValue(this string s)
        {
            return !string.IsNullOrEmpty(s);
        }
        public static string Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public static string Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }

        public static UserVM GetLogin()
        {
            var cookie = HttpContext.Current.Request.Cookies;
            if (cookie.Count > 0)
            {
                var userStr = Decode(cookie.Get("user") != null ? (string)cookie["user"].Value : "");
                var user = JsonConvert.DeserializeObject<UserVM>(userStr);
                if (user == null)
                {
                    return new UserVM();
                }
                else
                {
                    return user;
                }
            }
            else
            {
                return new UserVM();
            }

        }
        public static void SetLogin(UserVM model)
        {
            var encodeStr = Encode(Newtonsoft.Json.Linq.JObject.FromObject(model).ToString());
            var cookie = new HttpCookie("user", encodeStr);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static Lisans LisansOku()
        {
            Lisans result = 0;
            using (DBEntities db = new DBEntities())
            {
                string SqlString = "[dbo].[MPOS_LisansKontrol]";
                string constr = db.Database.Connection.ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(SqlString, con))
                    {
                        cmd.CommandType = System.Data.CommandType.Text;
                        con.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            string res = "";
                            while (reader.Read())
                            {
                                try
                                {
                                    res = reader["Lisans"].ToSafeString();
                                    if (res == "0")
                                    {
                                        con.Close();
                                        result = Lisans.Lisanssiz;
                                    }
                                    else
                                    {
                                        result = Lisans.Lisansli;
                                    }
                                }
                                catch
                                {
                                    con.Close();
                                    result = Lisans.Lisanssiz;
                                }
                            }
                            con.Close();
                            return result;
                        }
                    }
                }
            }
        }

    }
    public static class CacheHelper
    {
        public static void SaveToMemory(string key,object value,int day)
        {
            var dt = new DateTimeOffset();
            dt.AddDays(day);
            System.Runtime.Caching.MemoryCache.Default.Add(key, value,dt);
        }
        public static object ReadFromMemory(string key)
        {
            return System.Runtime.Caching.MemoryCache.Default.Get(key,null);
        }
    }
    public class CookiesHelper
    {

        public void SetCookie(string name = "", string value = "")
        {
            try
            {
                var cookie = new HttpCookie(name, Helpers.Encode(value));
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            catch (Exception ex)
            {

            }
        }
        public string GetCookie(string name = "")
        {
            try
            {
                var cookie = HttpContext.Current.Request.Cookies;
                if (cookie.Count > 0)
                {
                    var response = Helpers.Decode(cookie.Get(name) != null ? (string)cookie[name].Value : "");
                    return response;
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }
    }
}