# -*- coding: utf-8 -*-

# Copyright (C) 2010-2011
#   Drakmail < drakmail@gmail.com >
#   NomerUNO < uno.kms@gmail.com >
#   Platon Peacel☮ve <platonny@ngs.ru>
#   Elec.Lomy.RU <Elec.Lomy.RU@gmail.com>
#   ADcomp <david.madbox@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.

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

import os
from .xdg import CONFIG_FILE

def PATH_search(name):
    '''Search binary for PATH.'''
    if os.path.basename(name) != name:
        fullpath = [name]
    else:
        fullpath = [os.path.join(path, name)
                    for path in os.getenv('PATH', '').split(':')]
    for path in fullpath:
        if os.path.exists(path) and os.access(path, os.X_OK):
            return os.path.abspath(path)
    else:
        return None


class ConfigController():
    def __init__(self):
        if PATH_search('x-terminal-emulator'):
            terminal = 'x-terminal-emulator'
        else:
            terminal = 'xterm'
        self.default = {'terminal': terminal,
                        'systray': 'true',
                        'rounded': '4',
                        'menu_width': '430',
                        'bg_color': '#DCDCDC',
                        'border_color': '#4D4D4D',
                        'hide_list': 'None',
                        'category_click': 'true',
                        'favorites': 'false',
                        'desktop_dirs': ["/usr/share/applications/",
                                        "%s/.local/share/applications/"
                                        % os.getenv('HOME'),
                                        "/usr/local/share/applications/",
                                        "/usr/share/applications/kde4/",
                                        "/usr/local/share/applications/kde4/"
                                        ]
                        }
        config = {}
        try:
            f = open(CONFIG_FILE, 'r')
            print("Found user config.")
            for line in f:
                if line == '\n' or line.startswith('#'):
                    continue
                else:
                    line = line.strip('\n ').split('=')
                    if line[0] == "desktop_dirs":
                        config["desktop_dirs"] = filter( None, map ( lambda ln: ln.lstrip().rstrip(),
                            line[1].replace( "~",os.getenv("HOME") ).split(",") ) )
                    else:
                        config[line[0]] = line[1]
            f.close()
        except IOError,e:
            print("I/O error: %s" % repr(e))
        except:
            print("Error reading config file.")
        self.config = config
        self.validateConfig()

    #Check, if string is _valid_ integer.
    def isValidInt(self, string):
        try:
            int(string.strip().split()[0])
        except (ValueError, IndexError):
            return False
        return True

    def validateConfig(self):
        for line in self.config:
            if line not in self.default:
                print("Error in config file: Unknown parameter (\"{0}\")".format(line))
                continue
            if self.config[line] == '':
                print("Error in config file: parameter \"{0}\" can't be empty. Using default value.".format(line))
                self.config[line] = self.default[line]
            if line == 'rounded' or line == 'menu_width':
                if not self.isValidInt(self.config[line]):
                    print("Error in config file: parameter \"{0}\" must be valid integer. Using default value.".format(line))
                    self.config[line] = self.default[line]
                if int(self.config[line]) < 0:
                    print("Error in config file: parameter \"{0}\" must be positive value. Using default value.".format(line))
                    self.config[line] = self.default[line]

    def getValue(self, parameter):
        if parameter in self.config:
            return self.config[parameter]
        else:
            return self.default[parameter]
