using CloudGaming.AppConfigModel;
using CloudGaming.Code.AppExtend;
using CloudGaming.DtoModel;

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;

using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;

namespace CloudGaming.Web.Pages.Privacy;

/// <summary>
/// 隐私协议配置  
/// </summary>
public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;
    public string Identifier { get; set; }
    // 用于存储 HTML 内容
    public string PrivacyHtml { get; set; }
    /// <summary>
    ///  
    /// </summary>
    private IServiceProvider ServiceProvider { get; set; }

    public IndexModel(ILogger<IndexModel> logger, IServiceProvider _serviceProvider)
    {
        _logger = logger;
        ServiceProvider = _serviceProvider;
    }

    // 捕获 URL 中的 "identifier" 参数
    public void OnGet(string identifier)
    {
        if (string.IsNullOrEmpty(identifier))
        {
            return;
        }
        if (identifier.ToLower() == "cloudgaming")
        {
            identifier = "default";
        }

        var app = AppConfigurationExtend.GetAppConfigIdentifier(identifier);
        if (app == null)
        {
            return;
        }
        var appConfig = ServiceProvider.GetRequiredService<AppConfig>();
        app.ToAppConfig(appConfig);
        CloudGamingBase cloudGamingBase = new CloudGamingBase(ServiceProvider);
        var config = cloudGamingBase.Dao.DaoExt.Context.T_App_Config.Where(it => it.ConfigType == (int)AppConfigType.协议配置 && it.ConfigId == 2).FirstOrDefault();

        PrivacyHtml = config?.ConfigValue ?? "隐私协议";  // 保存 HTML 内容

        Identifier = identifier ?? "default";  // 如果没有提供 identifier 参数，默认为 "default"
    }
}
