using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineEnrollment.Shared
{
    public class AcumenRazorViewEngine : RazorViewEngine
    {
        public AcumenRazorViewEngine()
            : base()
        {
            ViewLocationCache = new DefaultViewLocationCache();
            //List<string> viewLocations = new List<string>(this.ViewLocationFormats);
            //viewLocations.Add("~/../Shared/Views/{0}/{1}.cshtml");
            //this.ViewLocationFormats = viewLocations.ToArray();
            //this.PartialViewLocationFormats = viewLocations.ToArray();            
        }

        public AcumenRazorViewEngine(IViewPageActivator viewPageActivator)
            : base(viewPageActivator)
        {
            ViewLocationCache = new DefaultViewLocationCache();
        }

        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            ViewEngineResult result = base.FindPartialView(controllerContext, partialViewName, useCache);
            if (result.View == null && controllerContext.Controller.TempData.ContainsKey("Base"))
            {
                string controllerName = controllerContext.RouteData.Values["controller"].ToString();
                object areaName = controllerContext.RouteData.DataTokens["area"];
                if (areaName != null)
                {
                    string path = string.Format("/Areas/Shared/Views/{0}/{1}.cshtml", controllerName, partialViewName);
                    if (!System.IO.File.Exists(controllerContext.HttpContext.Server.MapPath(path)))
                    {
                        path = string.Format("/Areas/Shared/Views/Shared/{1}.cshtml", controllerName, partialViewName);
                    }
                    IView view = this.CreatePartialView(controllerContext, path);
                    if (view != null)
                    {
                        result = new ViewEngineResult(view, this);
                    }
                }
            }
            return result;
        }

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            ViewEngineResult result = base.FindView(controllerContext, viewName, masterName, useCache);
            if (result.View == null && controllerContext.Controller.TempData.ContainsKey("Base"))
            {
                string controllerName = controllerContext.RouteData.Values["controller"].ToString();
                object areaName = controllerContext.RouteData.DataTokens["area"];
                if (areaName != null)
                {
                    string path = string.Format("/Areas/Shared/Views/{0}/{1}.cshtml", controllerName, viewName);
                    IView view = this.CreatePartialView(controllerContext, path);
                    if (view != null)
                    {
                        result = new ViewEngineResult(view, this);
                    }
                }
            }
            return result;
        }

        //    Protected Overrides Function CreatePartialView(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal partialPath As String) As System.Web.Mvc.IView
        //        Dim view As IView = MyBase.CreatePartialView(controllerContext, partialPath)
        //        Return view
        //    End Function

        //    Protected Overrides Function FileExists(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal virtualPath As String) As Boolean
        //        Return MyBase.FileExists(controllerContext, virtualPath)
        //    End Function

        //    Protected Overrides Function CreateView(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal viewPath As String, ByVal masterPath As String) As System.Web.Mvc.IView
        //        Return MyBase.CreateView(controllerContext, viewPath, masterPath)
        //    End Function

        //    Public Overrides Function FindPartialView(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal partialViewName As String, ByVal useCache As Boolean) As System.Web.Mvc.ViewEngineResult
        //        Dim view As IView = Nothing
        //        Dim result As ViewEngineResult = MyBase.FindPartialView(controllerContext, partialViewName, useCache)

        //        If result.View Is Nothing And controllerContext.Controller.TempData.ContainsKey("documentId") Then
        //            Dim documentId As Integer = controllerContext.Controller.TempData("documentId")
        //            Dim path As String = GetPage(documentId, useCache, controllerContext)

        //            If Not String.IsNullOrEmpty(path) Then
        //                view = CreatePartialView(controllerContext, path)
        //                result = New ViewEngineResult(view, Me)
        //            End If
        //        End If

        //        Return result
        //    End Function

        //    Public Overrides Function FindView(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal viewName As String, ByVal masterName As String, ByVal useCache As Boolean) As System.Web.Mvc.ViewEngineResult
        //        Dim view As IView = Nothing
        //        Dim result As ViewEngineResult = MyBase.FindView(controllerContext, viewName, masterName, useCache)

        //        If result.View Is Nothing And controllerContext.Controller.TempData.ContainsKey("documentId") Then
        //            Dim documentId As Integer = controllerContext.Controller.TempData("documentId")
        //            Dim path As String = GetPage(documentId, useCache, controllerContext)

        //            If Not String.IsNullOrEmpty(path) Then
        //                view = CreatePartialView(controllerContext, path)
        //                result = New ViewEngineResult(view, Me)
        //            End If

        //            Dim masterPath As String = GetMasterPage(controllerContext, useCache) '"/Documents/AsteryxRoot/MasterPages/AsteryxMaster.vbhtml"
        //            view = CreateView(controllerContext, path, masterPath)
        //            result = New ViewEngineResult(view, Me)
        //        End If

        //        Return result
        //    End Function

        //    Private Function GetPage(ByVal documentId As Integer, ByVal useCache As Boolean, ByVal controllerContext As ControllerContext) As String
        //        Dim cacheKey As String = GetCacheKey(documentId)
        //        Dim path As String = String.Empty

        //        If useCache Then
        //            path = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, cacheKey)
        //        End If

        //        If String.IsNullOrEmpty(path) Then
        //            Dim DataRepository As New ExplorerDataRepository()
        //            Dim document As Document = DataRepository.GetDocument(documentId)
        //            If document IsNot Nothing Then
        //                path = document.Path
        //                ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, path)
        //            End If
        //        End If

        //        Return path
        //    End Function

        //    Public Function GetMasterPage(ByVal controllerContext As ControllerContext, ByVal useCache As Boolean) As String
        //        If Not controllerContext.IsChildAction Then
        //            Dim DataRepository As New OrganizationDataRepository()
        //            Dim siteId As Integer = controllerContext.Controller.TempData("siteId")
        //            Dim masterPageId As Integer = DataRepository.GetSiteResource(siteId, My.Resources.ResourceMasterPage).Value
        //            Return GetPage(masterPageId, useCache, controllerContext)
        //        Else
        //            Return String.Empty
        //        End If
        //    End Function

        //    Public Overrides Sub ReleaseView(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal view As System.Web.Mvc.IView)
        //        MyBase.ReleaseView(controllerContext, view)
        //    End Sub

        //    Private Function GetCacheKey(ByVal documentId As Integer) As String
        //        Return String.Format("ViewCacheEntry:{0}:{1}", "document", documentId.ToString())
        //    End Function
    }
}