using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Qusion.Cms.Site;
using Qusion.Cms.Site.Core;
using Qusion.Cms.Users.Core;
using Qusion.Framework.Data;

namespace Qusion.WebApp.Areas.Qusion.Controllers
{
    public class InstallController : Controller
    {
        private readonly PageService _pageService;
        private readonly UserService _userService;
        private readonly SiteService _siteService;
        private readonly TemplateService _templateService;

        public InstallController(PageService pageService, UserService userService, SiteService siteService,
                                 TemplateService templateService)
        {
            //CreateDatabase();
            _pageService = pageService;
            _userService = userService;
            _siteService = siteService;
            _templateService = templateService;
        }

        public ActionResult CreateAll()
        {
            //var builder = new SessionBuilder();
            //builder.CreateDatabase();


            var all = Membership.GetAllUsers();
            foreach (MembershipUser user in all)
            {
                Membership.DeleteUser(user.UserName, true);
            }
            return View("Created");
        }


        public ActionResult Create()
        {
            var builder = new SessionBuilder();
            builder.CreateDatabase();
            return View();
        }

        public ActionResult ClearMembers()
        {
            var all = Membership.GetAllUsers();
            foreach (MembershipUser user in all)
            {
                Membership.DeleteUser(user.UserName, true);
            }
            return View();
        }

        [HttpGet]
        public ActionResult InitialData()
        {
            return View();
        }

        [HttpPost]
        public ActionResult InitialData(HttpPostedFileBase file)
        {
            // ensure site
            var site = _siteService.GetCurrent();
            // create template
            var template = _templateService.Import(Path.GetFileNameWithoutExtension(file.FileName),
                                                   file.InputStream);

            // root page
            var root = new Page
                           {
                               Alias = "home",
                               Title = "Home",
                               SiteInfo = site,
                               Template = template
                           };
            _pageService.Save(root);
            // set this page as root
            _siteService.SetRoot(root);
            // add child page for root
            root.Children.Add(new Page
                                  {
                                      Alias = "about",
                                      Title = "about",
                                      SiteInfo = site,
                                      Parent = root
                                  });
            // add widgets
            AddWidgets(root);

            // save page
            _pageService.Save(root);

            return Content("Installed initial data");
        }

        private void AddWidgets(Page page)
        {
            var widget1 = new Widget() {Section = "Body", Title = "Widget 1", Page = page};
            var widget2 = new Widget() {Section = "Body", Title = "Widget 2", Page = page};
            var widget3 = new Widget() {Section = "Side", Title = "Widget 3", Page = page};
            widget1.WidgetData.SetValue("controller", "test");
            widget1.WidgetData.SetValue("action", "index");
            widget2.WidgetData.SetValue("controller", "test2");
            widget2.WidgetData.SetValue("action", "index");
            widget3.WidgetData.SetValue("controller", "test");
            widget3.WidgetData.SetValue("action", "index");
            page.Widgets.Add(widget1);
            page.Widgets.Add(widget2);
            page.Widgets.Add(widget3);
        }

        public ActionResult AddResources(Guid id)
        {
            var page = _pageService.GetById(id);
            page.Resources.Add(new ScriptResource
                                   {
                                       Scope = PageResourceScope.Head,
                                       Type = "text/javascript",
                                       Src = "/qusion/headscript.js"
                                   });

            page.Resources.Add(new ScriptResource
                                   {
                                       Scope = PageResourceScope.Body,
                                       Type = "text/javascript",
                                       Src = "/qusion/bodyscript.js"
                                   });

            page.Resources.Add(new StyleResource
                                   {
                                       Rel = "stylesheet",
                                       Href = "/content/site.css",
                                       Type = "text/css"
                                   });

            page.Resources.Add(new StyleResource
                                   {
                                       Rel = "stylesheet",
                                       Href = "/content/bootstrap-responsive.css",
                                       Type = "text/css"
                                   });
            //<meta name="description" content="Free Web tutorials">
            page.Resources.Add(new MetaTag
                                   {
                                       Name = "description",
                                       Content = "Best CMS ever"
                                   });
            _pageService.Save(page);
            return Content("Resources added");
        }
    }
}