﻿using DomainWiser.Entities;
using DomainWiser.Entities.service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DomainWiser.Controllers
{
    public class QuestionController : Controller
    {
        QuestionService service = new QuestionService(new API.DAL.MongoContext());
        //
        // GET: /Question/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Question/Details/5
        public ActionResult Details(string id)
        {
            var m = service.GetById(id);
            return View(m);
        }

        //
        // GET: /Question/Create
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Question/Create
        [HttpPost]
        [ValidateInput(false)]

        public ActionResult Create(Question question)
        {
            try
            {
                // TODO: Add insert logic here
                question.Creator = this.User.Identity.Name;
                var c = question.Content;
                var l = question.Keywords;
                if (l != null && l.Count == 1) {
                    question.Keywords = l[0].Split(',').ToList();
                }
                question.CreateDate = DateTime.Now;
                service.Create(question);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Question/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Question/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Question/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Question/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
