import sys, os
from . import Path, Put, Snapshot

import time

try:
    from .config import DELAY_INTERVAL 
except ImportError as error:
    sys.exit('Please DELAY_INTERVAL in configure .config.py')

try:
    from .utils import send_email
except ImportError as error:
    sys.exit('Please setup email module in .utils.py')

try:
    import .utils as utils
except ImportError:
    print('No utility found, using system defaults')
    import sentinel.utils as utils
finally:

    __all__ = ['Sentinel']

    class Sentinel:

        INTERVAL = DELAY_INTERVAL

        def __init__(self, src_path:Path, dst_path:Path):
            self.src = src_path
            self.dst = dst_path

        def snap(self):
            """
            Returns a snapshot of the current src directory
            """
            return Snapshot(self.src)

        def changes(self, interval=INTERVAL):
            """
            Returns the changes in a given directory that happened 
            within the specified interval
            """
            snap1 = self.snap()
            time.sleep(interval)
            snap2 = self.snap()
            return snap1.compare(snap2)

        def transfer_files(self):
            """
            If changes are found, place them in the Put directory
            """
            changes = self.changes()
            for status, files in changes.items():
                if status == 'created':
                    for f in files:
                        print(f)
                        print(self.dst)
                        try:
                            Put(f, self.dst).put()
                            utils.send_email(f'{f.__class__.__name__} Transfer Found', f.__str__())
                        except Exception as e:
                            utils.send_email(f'{f.__class__.__name__} Transfer Found', f'AN ERROR OCCURED: {e} ')
                            print(e)

        def run(self):
            """
            Consistant loop of transfer_files
            """
            while True:
                self.transfer_files()


        
