# main.py
#
# Copyright 2024 Lucas Fryzek
# Based on code from QWeeChat
# Copyright (C) 2011-2022 Sébastien Helleu <flashcode@flashtux.org>
#
# 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 <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Configuration for Weegtk."""

import configparser
import os

from gi.repository import GLib

from pathlib import Path

CONFIG_DIR = f"{GLib.get_user_config_dir()}/weegtk"
CONFIG_FILENAME = f"{CONFIG_DIR}/weegtk.conf"

CONFIG_DEFAULT_RELAY_LINES = 50

CONFIG_DEFAULT_SECTIONS = ('relay', 'look', 'color', 'ssh', 'upload')
CONFIG_DEFAULT_OPTIONS = (('relay.hostname', ''),
                          ('relay.port', ''),
                          ('relay.ssl', "off"),
                          ('relay.password', ''),
                          ('relay.autoconnect', "off"),
                          ('relay.lines', str(CONFIG_DEFAULT_RELAY_LINES)),
                          ('ssh.host', ''),
                          ('ssh.port', ''),
                          ('ssh.username', ''),
                          ('ssh.key', ''),
                          ('upload.url', ''),
                          ('look.debug', 'off'),
                          ('look.statusbar', 'off'))

# Default colors for WeeChat color options (option name, #rgb value)
CONFIG_DEFAULT_COLOR_OPTIONS = (
    ('separator', '#000066'),  # 0
    ('chat', '#000000'),  # 1
    ('chat_time', '#999999'),  # 2
    ('chat_time_delimiters', '#000000'),  # 3
    ('chat_prefix_error', '#FF6633'),  # 4
    ('chat_prefix_network', '#990099'),  # 5
    ('chat_prefix_action', '#000000'),  # 6
    ('chat_prefix_join', '#00CC00'),  # 7
    ('chat_prefix_quit', '#CC0000'),  # 8
    ('chat_prefix_more', '#CC00FF'),  # 9
    ('chat_prefix_suffix', '#330099'),  # 10
    ('chat_buffer', '#000000'),  # 11
    ('chat_server', '#000000'),  # 12
    ('chat_channel', '#000000'),  # 13
    ('chat_nick', '#000000'),  # 14
    ('chat_nick_self', '*#000000'),  # 15
    ('chat_nick_other', '#000000'),  # 16
    ('', '#000000'),  # 17 (nick1 -- obsolete)
    ('', '#000000'),  # 18 (nick2 -- obsolete)
    ('', '#000000'),  # 19 (nick3 -- obsolete)
    ('', '#000000'),  # 20 (nick4 -- obsolete)
    ('', '#000000'),  # 21 (nick5 -- obsolete)
    ('', '#000000'),  # 22 (nick6 -- obsolete)
    ('', '#000000'),  # 23 (nick7 -- obsolete)
    ('', '#000000'),  # 24 (nick8 -- obsolete)
    ('', '#000000'),  # 25 (nick9 -- obsolete)
    ('', '#000000'),  # 26 (nick10 -- obsolete)
    ('chat_host', '#666666'),  # 27
    ('chat_delimiters', '#9999FF'),  # 28
    ('chat_highlight', '#3399CC'),  # 29
    ('chat_read_marker', '#000000'),  # 30
    ('chat_text_found', '#000000'),  # 31
    ('chat_value', '#000000'),  # 32
    ('chat_prefix_buffer', '#000000'),  # 33
    ('chat_tags', '#000000'),  # 34
    ('chat_inactive_window', '#000000'),  # 35
    ('chat_inactive_buffer', '#000000'),  # 36
    ('chat_prefix_buffer_inactive_buffer', '#000000'),  # 37
    ('chat_nick_offline', '#000000'),  # 38
    ('chat_nick_offline_highlight', '#000000'),  # 39
    ('chat_nick_prefix', '#000000'),  # 40
    ('chat_nick_suffix', '#000000'),  # 41
    ('emphasis', '#000000'),  # 42
    ('chat_day_change', '#000000'),  # 43
)
config_options = {"color": []}


def read():
    """Read config file."""
    global config_options
    config = configparser.RawConfigParser()
    if os.path.isfile(CONFIG_FILENAME):
        config.read(CONFIG_FILENAME)

    # add missing sections/options
    for section in CONFIG_DEFAULT_SECTIONS:
        if not config.has_section(section):
            config.add_section(section)
    for option in reversed(CONFIG_DEFAULT_OPTIONS):
        section, name = option[0].split('.', 1)
        if not config.has_option(section, name):
            config.set(section, name, option[1])
    section = 'color'
    for option in reversed(CONFIG_DEFAULT_COLOR_OPTIONS):
        if option[0] and not config.has_option(section, option[0]):
            config.set(section, option[0], option[1])

    # build list of color options
    config_options = {"color": []}
    for option in CONFIG_DEFAULT_COLOR_OPTIONS:
        if option[0]:
            config_options["color"].append(config.get('color', option[0]))
        else:
            config_options["color"].append('#000000')

    return config


def write(config):
    """Write config file."""
    Path(CONFIG_DIR).mkdir(mode=0o0700, parents=True, exist_ok=True)
    with open(CONFIG_FILENAME, 'w') as cfg:
        config.write(cfg)


def color_options():
    """Return color options."""
    global config_options
    return config_options["color"]

def str_to_bool(value):
    truthy = ["true", "1", "on", "y", "yes", "t", "i"]
    #falsy = ["false", "0", "off", "n", "no", "f", "o"]

    lower_val = value.lower()
    if lower_val in truthy:
        return True
    else:
        return False