using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using TABS.MailCarrier.WebSDK;
using TABS.MailCarrier.StgBase;
using TABS.MailCarrier.WebSDK.User;

public partial class Options_WebPush : System.Web.UI.Page
{
    protected string _apply = "";
    protected string _publicKey = "";
    protected string _swjs = "";

    protected override void InitializeCulture()
    {
        string defLang = WebUser.CurrentUser.UiLanguage;
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(defLang);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(defLang);
        base.InitializeCulture();
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        WebUtil.SetMcCommonCss(Page.Header);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection conn = new SqlConnection(GlobalConfig.ConnString))
            {
                conn.Open();
                Mcsp.McGlobalConfigGetValue(conn, "WebPushApply", out _apply);
                Mcsp.McGlobalConfigGetValue(conn, "WebPushAppServerPublicKey", out _publicKey);
                if (_apply != "true" || _publicKey.Length < 10)
                {
                    WebPushLabel.Text = GetLocalResourceObject("Msg05").ToString();
                    SubscribeButton.Enabled = false;
                    UnsubscribeButton.Enabled = false;
                }
            }
            if (WebUser.CurrentUser.UiLanguage == "ko")
                _swjs = "sw_kr.js";
            else
                _swjs = "sw.js";

            if (MaintenanceLicenseCheck.IsValid())
                WebPushFuncLabel.Text = GetLocalResourceObject("WebPushFunc1").ToString();
            else
                WebPushFuncLabel.Text = GetLocalResourceObject("WebPushFunc2").ToString();
        }
    }    
        
    #region AJAX Methods

    [WebMethod]
    public static bool Subscribe(string scope, string endpoint, string subscription)
    {
        using (SqlConnection conn = new SqlConnection(GlobalConfig.ConnString))
        {
            conn.Open();
            Mcsp.WpuUserSubscriptionAdd(conn, null, WebUser.CurrentUser.DomainUserID, scope, subscription, endpoint);
        }
        return true;
    }

    [WebMethod]
    public static bool Unsubscribe(string scope, string endpoint)
    {
        using (SqlConnection conn = new SqlConnection(GlobalConfig.ConnString))
        {
            conn.Open();
            Mcsp.WpuUserSubscriptionAdd(conn, null, WebUser.CurrentUser.DomainUserID, scope, "", endpoint);
        }
        return true;
    }

    #endregion
}