'''

Copyright (C) 2017-2018 The Board of Trustees of the Leland Stanford Junior
University.
Copyright (C) 2017-2018 Vanessa Sochat.

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

'''

from scif.logger import bot
from scif.main.parser import ( load_filesystem, load_recipe )
import os
import sys

bot.level = 3

class ScifRecipe:
    '''Create and work with a scif recipe. Usage typically looks like:

       recipe = ScifRecipe('sregistry.scif')
       
       Parameters
       ==========
       path: the path to the scif recipe flie.

    '''
    
    def __init__(self, path=None, app=None, writable=True, quiet=False):
        '''initialize the scientific filesystem by creating a scif folder
           at the base, and loading the recipe to fill it.

           Parameters
           ==========
           path: is the first paramter, not required to initialize an empty 
                 client session. The logic proceeds as follows:
                 1. If path is not defined, we want (empty) interactive session
                 2. We derive the base from the environment SCIF_BASE
        '''

        # 0. base determined from environment
        from scif.defaults import SCIF_BASE
        self.set_defaults()

        # If recipe path not provided, try default base
        if path is None:
            path = SCIF_BASE

        # 1. Determine if path is a recipe or base
        if path is not None:

            self.set_base(SCIF_BASE, writable=writable) # /scif
            self.load(path, app, quiet)                 # recipe, environment

        # 2. Neither, development client
        else:
            bot.info('[skeleton] session!')
            bot.info('           load a recipe: client.load("recipe.scif")')
            bot.info('           change default base:  client.set_base("/")')


    def __str__(self):
        return '[scif]'

    def __repr__(self):
        return '[scif]'


    def speak(self):
        '''the client should announce self given that the shell is being used.
        '''
        if self._base is not None:
            apps = " | ".join(self.apps())
            bot.custom(prefix="%s %s" %(self, self._base), 
                                        message=apps, 
                                        color="CYAN")
        else:
            bot.info(self)


    def load(self, path, app=None, quiet=False):
        '''load a scif recipe into the object

        Parameters
        ==========
        path: the complete path to the config (recipe file) to load, or 
              root path of filesystem (that from calling function defaults to
              /scif)
        app:  if running with context of an active app, this will load the
              active app environment for it as well.
        '''
        # 1. path is a recipe
        if os.path.isfile(path):
            self._config = load_recipe(path)

        # 2. path is a base
        elif os.path.isdir(path):
            self._config = load_filesystem(path, quiet=quiet)

        else:
            bot.warning('%s is not detected as a recipe or base.' %path)
            self._config = None

        self.update_env(app)
