import os
from operator import itemgetter

import click
import yaml

from requests.auth import HTTPBasicAuth


class Config(object):
    CONFIG_DIR = os.path.expanduser('~/.accountable')
    CONFIG_FILE = '{}/config.yaml'.format(CONFIG_DIR)
    DEFAULT_ISSUE_FIELDS = [
        {'reporter': 'displayName'},
        {'assignee': 'displayName'},
        {'issuetype': 'name'},
        {'status': {'statusCategory': 'name'}},
        'summary',
        'description'
    ]
    DEFAULT_ALIASES = {'cob': 'checkoutbranch',
                       'co': 'checkout'}

    def __init__(self, **kwargs):
        if kwargs.get('create_config'):
            self._initial_setup(kwargs)
        self.config = self._load_config()
        self.username, self.password, self.domain, self.issue_fields = \
            itemgetter(
                'username', 'password', 'domain', 'issue_fields'
            )(self.config)
        self.auth = HTTPBasicAuth(self.username, self.password)

    def _load_config(self):
        with open(self.CONFIG_FILE, 'r') as f:
            config = yaml.load(f)
        return config

    def _initial_setup(self, kwargs):
        username = str(kwargs.get('username'))
        password = str(kwargs.get('password'))
        domain = str(kwargs.get('domain'))
        config_dict = self._config_dict(username, password, domain)
        self._create_config(config_dict)

    def _create_config_dir(self):
        if not os.path.exists(self.CONFIG_DIR):
            click.echo('Creating {}'.format(self.CONFIG_DIR))
            os.makedirs(self.CONFIG_DIR)

    def _config_dict(self, username, password, domain):
        config = {
            'username': username,
            'password': password,
            'domain': domain,
            'issue_fields': self.DEFAULT_ISSUE_FIELDS,
            'aliases': self.DEFAULT_ALIASES
        }
        return config

    def _create_config(self, config_dict):
        self._create_config_dir()
        with open(self.CONFIG_FILE, 'w') as f:
            f.write(yaml.dump(config_dict, default_flow_style=False))
        click.echo('Configuration file written to {}'.format(self.CONFIG_FILE))

@@ -599,79 +599,28 @@
     
-if 
+self.
 kwargs
-.get('create_config'):%0A            self._initial_setup(
+ = 
 kwargs
-)
 %0A   
@@ -625,24 +625,25 @@
        self.
+_
 config = sel
@@ -643,27 +643,12 @@
 g = 
-self._load_config()
+None
 %0A   
@@ -898,16 +898,214 @@
 sword)%0A%0A
+    @property%0A    def config(self):%0A        if self.kwargs.get('create_config'):%0A            self._initial_setup(self.kwargs)%0A        self._config = self._load_config()%0A        return self._config%0A%0A
     def 
@@ -2119,16 +2119,17 @@
 FILE, 'w
++
 ') as f:

Fix config loading