from sqlalchemy import Column, Integer, String, Boolean, DateTime, Text, ForeignKey, Unicode
from sqlalchemy.orm import relationship
from datetime import datetime
from database import Base
from database import db_session
from email.header import decode_header
from marshmallow import Schema, fields, post_load
import yaml
import email
from mail_model import Mail
#from fetch_mail import fetch_mail
import re


class FullThreadSchema(Schema):
    id=fields.Integer()
    text=fields.String()
    body=fields.String()
    envelope=fields.String()


class MailThread(Base):
    __tablename__ = 'threads'
    id = Column(Integer, primary_key=True)
    firstmail  = Column(Integer)
    date = Column(DateTime)
    islabeled = Column(Boolean)
    istrained = Column(Boolean)
    opened = Column(Boolean)
    body = Column(Text)
    maintopic=Column(String)
    lang=Column(String)
    answered=Column(String)
    __schema__=FullThreadSchema
    __jsonid__='thread'
    __whiteattrs__= ["body"]
    __jsonattrs__=None
#    answered=False
#    maintopic="information"
#    lang=""
    def bdy(self):
        return yaml.load(self.body)
    def is_answered(self):
        return self.answered in ["1", "true", "True", "t","T"]
    def to_text(self):
        mmm=self.mails()
        txt=""
        for m in mmm:
#            m.compile_envelope()
            txt=txt+"mail: \n"
            for f in yaml.load(m.from_):
                txt=txt+f["mail"]+"@"+f["host"]
            txt=txt+" --- "
            txt=txt+" ".join(yaml.load(m.subject))
            txt=txt+"\n"
        return txt

    def tstr(self):
        fr=yaml.load(self.mails()[0].from_)
        return "(" + str(self.opened)+ ", "+ str(self.maintopic)+ ", "+ str(self.lang) + ") " + str(self.firstmail)+": "+str(fr[0]["mail"])+"@"+str(fr[0]["host"]) + " | ".join(yaml.load(self.mails()[0].subject))
    
    def mails(self):
        a=[]
#        print self.bdy()
        for m in self.bdy():
            mail=db_session.query(Mail).filter(Mail.id==int(m)).first()
            if mail is None:
                mail=Mail.fetch_mail(int(m))
            a.append(mail)
        return a
        
    def mail_dicts(self):
        a=[]
#        print "maildicts: "+ str(self.mails())
        for m in self.mails():
#            m.compile_envelope()
            a.append(m.dict_envelope())
        return a

    def mail_flat_dict(self):
        a=[]
        d={}
        dc=self.mail_dicts()
#        print dc
        for i in range(0,len(dc)):
            for k, v  in dc[i].iteritems():
                d["mail_"+str(i)+"_"+k]=v
        for k, v  in dc[-1].iteritems():
            d["mail_last_"+k]=v
        
        return d

    def subject(self):
        a=""
        for m in self.mails():
            a=a + " ".join(yaml.load(m.subject))+"\n"

        return a

    def compile(self):
        for m in self.mails():
            m.compile_envelope()
            m.compile_text()
            db_session.add(m)
            db_session.commit()
        self.date=self.mails()[0].date

    def print_mail(self, filter="all"):
        a=[]
        if filter=="all":
            mm=self.mails()
            for m in mm:
                a.append((m.print_head(), m.print_text()))
        return a
        
    def print_text(self,filter="all"):
        a=[]
        if filter=="all":
            mm=self.mails()
            for m in mm:
                a.append(m.print_text())
        elif filter=="first":
            a.append(m[0].print_text())
        return a
    def print_head(self,filter="all"):
        a=[]
        if filter=="all":
            mm=self.mails()
            for m in mm:
                a.append(m.print_head())
        elif filter=="first":
            a.append(m[0].print_head())
        return a
    
    def text(self,filter="all"):
        a=u""
        def mail_txt(m):
            t=yaml.load(m.text)
            if type(t) is unicode:
                txt=t
            else:
                t=t.decode("ISO-8859-1")
                txt=t
            return txt
        mm=self.mails()
        if filter=="all":
            for m in mm:
                a=a+mail_txt(m)+"\n****........................................***\n"
        elif filter=="first":
            a=mail_txt(mm[0])
        a=re.sub(r'\n\s*\n',r'\n',a)
        a=re.sub(r'<!--.*-->',r'',a,flags=re.MULTILINE|re.DOTALL)
        a=re.sub(r'\s*>+ .*\n',r'',a)


            
        return a
