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

namespace Nateby.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Who()
        {
            return View();
        }

        public ActionResult What()
        {
            return View();
        }

        public ActionResult Enquire()
        {
            return View();
        }

        [CaptchaValidator]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Enquire(FormCollection callback, bool captchaValid)
        {
            if (callback["what"]=="callback") {
            //process the cllback form
                if (callback["contactname"].Trim().Length == 0)
                    ModelState.AddModelError("contactname", "");
                if (callback["telephone"].Trim().Length == 0)
                    ModelState.AddModelError("telephone", "");
                
                if (!ModelState.IsValid)
                    {
                        return View(callback);
                    }

                    else
                    {
                        string message = Environment.NewLine + "<BR>ContactName:" + callback["contactName"];
                        message += Environment.NewLine + "<BR>Telephone:" + callback["telephone"];


                        string clientmessage = "Good News! You have a new call back request from the website." + message;

                        //email the client
                        System.Net.Mail.MailMessage clientMail = new System.Net.Mail.MailMessage();
                        clientMail.From = new System.Net.Mail.MailAddress("website@nateby.co.uk");
                        clientMail.To.Add("offices@nateby.co.uk");
                        clientMail.Subject = "Website:  Call Back Request";
                        clientMail.Body = clientmessage;
                        clientMail.IsBodyHtml = true;
                        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("localhost");
                        client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                        client.Send(clientMail);

                        return Redirect("/thankyou");

                    }
            } else {
                //process the enquiry form
                if (callback["YourName"].Trim().Length == 0)
                    ModelState.AddModelError("YourName", "");
                if (callback["Email"].Trim().Length == 0)
                    ModelState.AddModelError("Email", "");
                if (!captchaValid)
                    ModelState.AddModelError("Captcha", "You did not type the verification word correctly. Please try again.");

                
                if (!ModelState.IsValid)
                    {
                        return View();
                    }

                    else
                    {
                        string message = Environment.NewLine + "<BR>ContactName:" + callback["YourName"];
                        message += Environment.NewLine + "<BR>CompanyName:" + callback["CompanyName"];
                        message += Environment.NewLine + "<BR>Address:" + callback["Address"];
                        message += Environment.NewLine + "<BR>Email:" + callback["Email"];
                        message += Environment.NewLine + "<BR>Telephone:" + callback["Telephone"];
                        message += Environment.NewLine + "<BR>Office Size:" + callback["office-size"];
                        message += Environment.NewLine + "<BR>Mailing List:" + callback["mailinglist"];
                        message += Environment.NewLine + "<BR>Comments:" + callback["Comments"];

                        string clientmessage = "Good News! You have a new enquiry request from the website." + message;

                        //email the client
                        System.Net.Mail.MailMessage clientMail = new System.Net.Mail.MailMessage();
                        clientMail.From = new System.Net.Mail.MailAddress("website@nateby.co.uk");
                        clientMail.To.Add("offices@nateby.co.uk");
                        clientMail.Subject = "Website:  Enquiry Request";
                        clientMail.Body = clientmessage;
                        clientMail.IsBodyHtml = true;
                        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("localhost");
                        client.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                        client.Send(clientMail);

                        return Redirect("/thankyou");

                    }
            }
        }

        public ActionResult Thankyou()
        {



                    return View();

                    
                

        }


         public ActionResult Office()
        {
            return View();
        }

        public ActionResult Where()
        {
            return View();
        }

        public ActionResult Terms()
        {
            return View();
        }

        public ActionResult Links()
        {
            return View();
        }
 
    }
}
