using System;
using System.Linq;
using IntraService.Domain.Models;
using IntraService.Domain;
using System.Configuration;
using IntraService.Domain.Services;

public partial class CheckSite : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var isHttps = Request.IsSecureConnection;
        var statusString = "";
        var ldapAuthentication = !String.IsNullOrEmpty(ConfigurationManager.AppSettings["LDAPAuthentication"]) && 
            ConfigurationManager.AppSettings["LDAPAuthentication"].ToUpperInvariant() == "TRUE";

        var name = (String.IsNullOrEmpty(Request["login"]) ? String.Empty : Request["login"]).Trim();
        var password = (String.IsNullOrEmpty(Request["password"]) ? String.Empty : Request["password"]).Trim();

        try
        {
            using (var dc = new IntraServiceDataContext())
            {
                if (!String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(password))
                {
                    var user = dc.Users.FirstOrDefault(x => x.Login.ToUpper() == name.ToUpper() && x.Password == password);

                    if (user == null && ldapAuthentication) //На случай, если пароль пользователя сменился в домене, а в базе ещё не обновился.
                    {
                        /*
                        var _loginService = new LoginService(dc, new SettingsService());
                        user = _loginService.GetLdapUser(name, password);
                        */
                        user = dc.Users.FirstOrDefault(x => x.Login.ToUpper() == name.ToUpper() && x.ADGuid.HasValue && !x.IsArchive);
                    }

                    if (user == null)
                        statusString = "wrong password";
                    else
                    {
                        statusString = "OK";
                    }
                }
                else
                {
                    var user = dc.Users.FirstOrDefault();
                    statusString = user != null ? "OK" : "Отсутсвует пользователи в БД";
                }
                if (!String.IsNullOrEmpty(Request["systemInformation"]))
                {
                    var isApiInclude = dc.SystemFields.Any(x => x.Key.ToLower() == "includeapi" && x.Value.ToLower() == "true");
                    var tariff = (TariffPlan)System.Web.HttpContext.Current.Application["tariff"];
                    var maxExecSetting = dc.SystemFields.FirstOrDefault(x => x.Key.ToLower() == "maxexecutors");
                    var maxExecSettingString = maxExecSetting == null ? "" : maxExecSetting.Value;
                    var maxExecutors = tariff == TariffPlan.Enterprise || tariff == TariffPlan.Professional ? IntraService.ResourceHelper.GeneralResource("SystemInfo.NoLimits")
                        : !String.IsNullOrEmpty(maxExecSettingString) ? maxExecSettingString
                        : tariff == TariffPlan.FreeLance ? "1" : tariff == TariffPlan.Free ? "3" : tariff == TariffPlan.SaaS ? "5" : tariff == TariffPlan.SaaSPlus ? "50" : "";
                    statusString = String.Format("{3}APIInclude:{0}|Tariff:{1}|MaxExecutors:{2}|", isApiInclude, tariff, maxExecutors, String.IsNullOrEmpty(statusString)?"":statusString+"|");
                }
            }
            Response.Write(String.Format("Status: {2} {1}HTTPS:{0}", isHttps, Environment.NewLine, statusString));
        }
        catch (Exception ex)
        {
            Response.Write(string.Format("Status: {2} {1}HTTPS:{0}", isHttps, Environment.NewLine, ex.Message));
        }
    }
}
