# core_on_login.py    chromatic universe     william k. johnson 2018

from time import sleep
#cci
from cci_imap_gadget.imap_gadget_base import cci_chilkat , \
                                             cci_ecosys , \
                                             cci_mini_imap_mail
from cci_imap_gadget.core_on_logout import on_logout

import cci_utils.cci_io_tools as io


# ----------------------------------------------------------------------------------------
class on_login( object ) :

            """
            on_login
            """

            def __init__( self ,
                          cci_chilkat = None ,
                          cci_mail = None ,
                          cci_ico  = None ) :

                        """
                        :param cci_chilkat:
                        :param cci_mail:
                        :param cci_ico:
                        """


                        # logging
                        self._logger = io.init_logging( self.__class__.__name__  )
                        self._logger.info( self.__class__.__name__ + '...'  )
                        self._cci = cci_chilkat
                        self._mail = cci_mail
                        self._ecosys = cci_ico
                        self._imap_states = set()
                        self._imap_states.add( 'non-authenticated' )


            @property
            def cci( self ) :
                return self._cci
            @cci.setter
            def cci( self , cc ) :
                self._cci = cc
            @property
            def mail( self ) :
                return self._mail
            @mail.setter
            def mail( self , m ) :
                self._mail = m
            @property
            def eco( self ) :
                return self._ecosys
            @eco.setter
            def eco( self , ec ) :
                self._eco = ec
            @property
            def logger( self ) :
                return self._logger
            @logger.setter
            def logger( self , log ) :
                self._logger = log
            @property
            def imap_states( self ) :
                return self._imap_states
            @imap_states.setter
            def imap_states( self , states ) :
                self._imap_states = states



            def perform( self ) :

                        """

                        :return:
                        """

                        # xoonnect to an imap server.
                        self.cci.imap.put_KeepSessionLog( True )
                        self.cci.imap.put_VerboseLogging( True )
                        print( self.mail.imap_addr )
                        b_ret = self.cci.imap.Connect( self.mail.imap_addr )
                        if b_ret :
                            #login
                            b_ret = self.cci.imap.Login( self.mail.imap_mail_account ,
                                                         self.mail.imap_mail_auth )
                            if not b_ret :
                                self.logger.error( '...login failed....')
                        #self.logger.info( self.cci.imap.lastErrorText() )
                        self.logger.info( self.cci.imap.sessionLog() )

                        self.imap_states.remove( 'non-authenticated' )
                        self.imap_states.add( 'authenticated' )


                        sleep( 2 )



# ----------------------------------------------------------------------------------------
if __name__ == '__main__' :

                        try :


                            #imap default params
                            mini_mail = cci_mini_imap_mail( mail_account="wiljoh" , mail_auth="Argentina1" )
                            mini_mail.logger.info( repr( mini_mail) )
                            #cci microkernel
                            mini_ecosys = cci_ecosys( mta_addr='127.0.0.1' ,
                                                      mda_addr='127.0.0.1' ,
                                                      imap_mail=mini_mail )
                            mini_ecosys.logger.info( repr( mini_ecosys ) )

                            #chilkat instances
                            cci = cci_chilkat()

                            for idx in range( 0 , 9 ) :
                                login = on_login( cci , mini_mail , mini_ecosys )
                                login.perform()

                                #logout
                                logout = on_logout( cci , mini_mail , mini_ecosys )
                                logout.perform()

                                sleep( 8 )


                        except Exception as e :
                            print( str( e ) )

