<commit_before>package web

import (
	"html/template"
	"log"
	"net/http"
	"path/filepath"
	"mdrobek/watney/mail"
	"sort"
	"encoding/json"
	"errors"
	"mdrobek/watney/conf"
	"strconv"
)

type MailWeb struct {
	// All initially parsed html templates
	templates map[string]*template.Template
	// Mail server configuration
	mconf *conf.MailConf
	// Mail server connection
	imapCon *mail.MailCon
	// Whether the app server is run in debugging mode for dev
	debug bool
}

func NewWeb(mailConf *conf.MailConf, debug bool) *MailWeb {
	var web *MailWeb = new(MailWeb)
	web.mconf = mailConf
	web.debug = debug

	var err error
	web.imapCon, err = mail.NewMailCon(web.mconf)
	if nil != err {
		panic("Couldn't establish connection to imap mail server: %s', err")
	}

	// init the map
	if nil == web.templates {
		web.templates = make(map[string]*template.Template)
	}

	strTmpls, err := filepath.Glob("src/mdrobek/watney/static/templates/*.html")
	if nil != err {
		log.Fatalf("Couldn't find templates: %s", err)
	}

	log.Print(strTmpls)
	web.templates["main"] = template.Must(template.ParseFiles(strTmpls...))

//	for _, curTmpl := range strTmpls {
//		web.templates[filepath.Base(curTmpl)] = template.Must(
//			template.ParseFiles(curTmpl))
//	}

	return web
}

/**
	Closes the IMAP mail server connection
 */
func (web *MailWeb) Close() error {
	if nil != web.imapCon {
		return web.imapCon.Close()
	}
	return errors.New("No imap connection was available to close")
}

/**************************************************************************************************
 ***									Web methods												***
 **************************************************************************************************/
func (web *MailWeb) Root(w http.ResponseWriter, r *http.Request) {
	web.renderTemplate(w, "main", map[string]interface{}{
		"IsDebug" : web.debug,
	})
}

func (web *MailWeb) Mails(w http.ResponseWriter, r *http.Request) {
	mc, err := mail.NewMailCon(web.mconf)
	defer mc.Close()
	var mails []mail.Mail = []mail.Mail{}
	if nil == err {
		switch r.FormValue("mailInformation") {
		case mail.FULL: mails, _ = mc.LoadMailsFromFolder(r.FormValue("folder"))
		case mail.OVERVIEW: fallthrough
		default: mails, _ = mc.LoadMailOverview(r.FormValue("folder"))
		}
		// Reverse the retrieved mail array
		sort.Sort(mail.MailSlice(mails))
	}
	jsonRet, err := json.Marshal(mails)
	if (nil != err) {
		log.Fatal(err)
	}
	w.Write(jsonRet)
}

func (web *MailWeb) MailContent(w http.ResponseWriter, r *http.Request) {
	mc, err := mail.NewMailCon(web.mconf)
	defer mc.Close()
	var mailContent string
	if nil == err {
		uid, _ := strconv.ParseUint(r.FormValue("uid"), 10, 32)
		mailContent, _ = mc.LoadContentForMail(uid)
	}
	jsonRet, err := json.Marshal(mailContent)
	if (nil != err) {
		log.Fatal(err)
	}
	w.Write(jsonRet)
}

//func (web *MailWeb) Headers(w http.ResponseWriter, r *http.Request) {
////	mc, err := mail.NewMailCon(web.mconf)
////	defer mc.Close()
//	var headers mail.HeaderSlice = mail.HeaderSlice{}
////	if nil == err {
//		headers, _ = web.imapCon.LoadMailHeaders()
//		//		mails, _ = mc.LoadNMails(4)
//		// Reverse the retrieved mail array
//		sort.Sort(headers)<commit_msg>MailContent Loading Backend - fixed problem that imap client wasn't in correct State to retrieve Mail content from Server<commit_after>package web

import (
	"html/template"
	"log"
	"net/http"
	"path/filepath"
	"mdrobek/watney/mail"
	"sort"
	"encoding/json"
	"errors"
	"mdrobek/watney/conf"
	"strconv"
)

type MailWeb struct {
	// All initially parsed html templates
	templates map[string]*template.Template
	// Mail server configuration
	mconf *conf.MailConf
	// Mail server connection
	imapCon *mail.MailCon
	// Whether the app server is run in debugging mode for dev
	debug bool
}

func NewWeb(mailConf *conf.MailConf, debug bool) *MailWeb {
	var web *MailWeb = new(MailWeb)
	web.mconf = mailConf
	web.debug = debug

	var err error
	web.imapCon, err = mail.NewMailCon(web.mconf)
	if nil != err {
		panic("Couldn't establish connection to imap mail server: %s', err")
	}

	// init the map
	if nil == web.templates {
		web.templates = make(map[string]*template.Template)
	}

	strTmpls, err := filepath.Glob("src/mdrobek/watney/static/templates/*.html")
	if nil != err {
		log.Fatalf("Couldn't find templates: %s", err)
	}

	log.Print(strTmpls)
	web.templates["main"] = template.Must(template.ParseFiles(strTmpls...))

//	for _, curTmpl := range strTmpls {
//		web.templates[filepath.Base(curTmpl)] = template.Must(
//			template.ParseFiles(curTmpl))
//	}

	return web
}

/**
	Closes the IMAP mail server connection
 */
func (web *MailWeb) Close() error {
	if nil != web.imapCon {
		return web.imapCon.Close()
	}
	return errors.New("No imap connection was available to close")
}

/**************************************************************************************************
 ***									Web methods												***
 **************************************************************************************************/
func (web *MailWeb) Root(w http.ResponseWriter, r *http.Request) {
	web.renderTemplate(w, "main", map[string]interface{}{
		"IsDebug" : web.debug,
	})
}

func (web *MailWeb) Mails(w http.ResponseWriter, r *http.Request) {
	mc, err := mail.NewMailCon(web.mconf)
	defer mc.Close()
	var mails []mail.Mail = []mail.Mail{}
	if nil == err {
		switch r.FormValue("mailInformation") {
		case mail.FULL: mails, _ = mc.LoadMailsFromFolder(r.FormValue("folder"))
		case mail.OVERVIEW: fallthrough
		default: mails, _ = mc.LoadMailOverview(r.FormValue("folder"))
		}
		// Reverse the retrieved mail array
		sort.Sort(mail.MailSlice(mails))
	}
	jsonRet, err := json.Marshal(mails)
	if (nil != err) {
		log.Fatal(err)
	}
	w.Write(jsonRet)
}

func (web *MailWeb) MailContent(w http.ResponseWriter, r *http.Request) {
	mc, err := mail.NewMailCon(web.mconf)
	defer mc.Close()
	var mailContent string
	if nil == err {
		uid, _ := strconv.ParseInt(r.FormValue("uid"), 10, 32)
		mailContent, _ = mc.LoadContentForMail(r.FormValue("folder"), uint32(uid))
	}
	jsonRet, err := json.Marshal(mailContent)
	if (nil != err) {
		log.Fatal(err)
	}
	w.Write(jsonRet)
}

//func (web *MailWeb) Headers(w http.ResponseWriter, r *http.Request) {
////	mc, err := mail.NewMailCon(web.mconf)
////	defer mc.Close()
//	var headers mail.HeaderSlice = mail.HeaderSlice{}
////	if nil == err {
//		headers, _ = web.imapCon.LoadMailHeaders()
//		//		mails, _ = mc.LoadNMails(4)
//		// Reverse the retrieved mail array
//		sort.Sort(headers)
{'fraction_non_alphanumeric': 0.20350985221674878, 'fraction_numerical': 0.0018472906403940886, 'mean_word_length': 4.083724569640062, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3977973', 'n_tokens_mistral': 2402, 'n_tokens_neox': 2255, 'n_words': 523}