using System;
using System.Linq;
using IntraService.Domain.Models;
using IntraService.Security;

public partial class _Default : System.Web.UI.Page
{
    public IIntraServicePrincipal Principal { get { return Context.User as IIntraServicePrincipal; } }
    protected IIntraServiceIdentity Identity { get { return Principal.Identity as IIntraServiceIdentity; } }
    protected void Page_Load(object sender, EventArgs e)
    {
        using(var dc = new IntraServiceDataContext())
        {
            if (Principal != null)
            {
                var user = dc.Users.SingleOrDefault(u => u.Id == Identity.Id);
                //var isTicketPage = IntraVision.Web.Settings.GetFromDatabase("IsTicketStartPage").ToUpperInvariant() == "TRUE";
                var isTicketPage = user != null && dc.RolePermissions.Any(rp => rp.Permission.Alias == "startpagetask" && rp.RoleId == user.RoleId); 
                if (user != null && !isTicketPage)
                {
                    Response.Redirect(dc.RolePermissions.Any(rp => rp.Permission.Alias == "showdashboard" && rp.RoleId == user.RoleId)? "~/dashboard.ivp": "~/Task/Index");
                    Response.Redirect("~/dashboard.ivp");
                }
                else Response.Redirect("~/Task/Index");
            }
            else Response.Redirect("~/dashboard.ivp");
        }
        
       
    }
}
