import yaml, tempfile, os, libUi
import PinshCmd, ConfigField
from bombardier_core.static_data import OK, FAIL
from SystemStateSingleton import SystemState
system_state = SystemState()

class CreateType(PinshCmd.PinshCmd):
    '''A thing that can be created.'''
    def __init__(self, name, help_text):
        PinshCmd.PinshCmd.__init__(self, name, help_text)
        self.config_field = None
        self.cmd_owner = 1

    def cmd(self, command_line):
        '''Create a thing that the user is interested in'''
        if command_line.no_flag:
            return FAIL, []
        if len(command_line) < 3:
            return FAIL, ["Incomplete command."]

        conf_str = self.config_field.get_default_data()
        #conf_str = yaml.dump(current_dict, default_flow_style=False)
        file_descriptor, file_name = tempfile.mkstemp(suffix=".yml", text=True)
        file_handle = os.fdopen(file_descriptor, 'w+b')
        file_handle.write(conf_str)
        file_handle.close()
        os.system("vim %s" % file_name)
        post_data = yaml.load(open(file_name).read())
        submit = libUi.ask_yes_no("Commit changes to server", libUi.YES)
        if submit:
            _status, output = self.config_field.post_specific_data(command_line, 2, post_data)
            os.unlink(file_name)
            return OK, [output]
        else:
            msg = "Discarded changes. Edits can be found here: %s" % file_name
            return OK, [msg]

class Machine(CreateType):
    'Create a default machine configuration data from the server'
    def __init__(self):
        CreateType.__init__(self, "machine", "machine\tcreate a new machine configuration")
        self.config_field = ConfigField.ConfigField(data_type=ConfigField.MACHINE, new=True)
        self.children = [self.config_field]

class Include(CreateType):
    'Create an include file on the server'
    def __init__(self):
        CreateType.__init__(self, "include", "include\tcreate a shared include file")
        self.config_field = ConfigField.ConfigField(data_type=ConfigField.INCLUDE, new=True)
        self.children = [self.config_field]

class User(CreateType):
    'Create a package file'
    def __init__(self):
        CreateType.__init__(self, "user", "user\tcreate a new user to log in to Bombardier")
        self.config_field = ConfigField.ConfigField(data_type=ConfigField.USER, new=True)
        self.children = [self.config_field]

class Package(CreateType):
    'Create a package file'
    def __init__(self):
        CreateType.__init__(self, "package", "package\tcreate new package metadata")
        self.config_field = ConfigField.ConfigField(data_type=ConfigField.PACKAGE, new=True)
        self.children = [self.config_field]

class Bom(CreateType):
    'create new bill-of-materials ("bom") file from the server'
    def __init__(self):
        CreateType.__init__(self, "bom", "bom\tcreate a bill of materials")
        self.config_field = ConfigField.ConfigField(data_type=ConfigField.BOM, new=True)
        self.children = [self.config_field]

class Create(PinshCmd.PinshCmd):
    '''Create a file'''
    def __init__(self):
        PinshCmd.PinshCmd.__init__(self, "create")
        self.help_text = "create\tcreate a new system component"
        machine = Machine()
        include = Include()
        package = Package()
        user = User()
        bom = Bom()
        self.children = [machine, include, bom, package, user]
        self.cmd_owner = 1
