#!/usr/bin/bash
# -*- mode: sh -*-
#
# YAD settings editor. This file is part of YAD
#
# YAD 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.
#
# YAD 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
# YAD. If not, see <https://www.gnu.org/licenses/>.
#
# Copyright (C) 2021-2026, Victor Ananjevsky <victor@sanana.kiev.ua>
#

TEXTDOMAIN=yad
TEXTDOMAINDIR=/usr/share/locale

res1=$(mktemp --tmpdir ys-tab1.XXXXXXXX)
res2=$(mktemp --tmpdir ys-tab2.XXXXXXXX)
trap "rm -f $res1 $res2" EXIT

KEY=$RANDOM

defconfig=${XDG_CONFIG_HOME:-$HOME/.config}/yad/settings.conf
filename=${YAD_SETTINGS_FILE:-$defconfig}

# parse settings file
declare -A Common
declare -A SourceView

yad --write-settings
while read ln; do
    if [[ ${ln:0:1} == [ ]]; then
        aname=${ln//[\[\]]}
    else
        k=${ln%=*}
        v=${ln#*=}
        eval ${aname}[$k]="${v@Q}"
    fi
done < <(grep -v -E "(^#.*|^[[:space:]]*)$" $filename)

# first tab (values at the end due to possible hyphens)
yad --plug=$KEY --tabnum=1 --bool-fmt=t --quoted-output --separator=" " \
    --num-output --form --scroll --homogeneous \
    --text=$"Main YAD settings and default values\n\n" \
    --field=$"Width of dialog window::NUM" \
    --field=$"Height of dialog window::NUM" \
    --field=$"Border width around dialog::NUM" \
    --field=$"Show remaining time and percentage in timeout progress bar:CHK" \
    --field=$"Combo-box in entry dialog is always editable:CHK" \
    --field=$"Default terminal command:" \
    --field=$"Default open command:" \
    --field=$"Default date format:" \
    --field=$"Default URI color in text-info dialog::CLR" \
    --field=$"Maximum number of tabs in notebook dialog::NUM" \
    --field=$"Use large previews in file selection dialogs:CHK" \
    --field=$"Search entry width::NUM" \
    --field=$"Ignore unknown command-line options:CHK" \
    --field=$"Enable debug mode with information about deprecated features:CHK" \
    -- \
    "${Common[Width]}!-1..32767" "${Common[Height]}!-1..32767" "${Common[Border]}" \
    "${Common[ShowRemain]}" "${Common[ComboAlwaysEditable]}" "${Common[Terminal]}" \
    "${Common[OpenCommand]}" "${Common[DateFormat]}" "${Common[URIColor]}" \
    "${Common[MaxTab]}" "${Common[LargePreview]}" "${Common[SearchWidth]}!-1..1024" \
    "${Common[IgnoreUnknownOptions]}" "${Common[Debug]}" > $res1 &

# get list for themes
themes=$"<Empty>!"
while read t; do
    [[ $t == ${SourceView[Theme]} ]] && themes+="^$t!" || themes+="$t!"
done < <(yad-tools --show-themes)

# get list for home/end
homend=(never before after always)
homend[${SourceView[HomeEnd]}]="^${homend[${SourceView[HomeEnd]}]}"
he_str=
for b in ${homend[@]}; do
    he_str+="$b!"
done

# second tab
yad --plug=$KEY --tabnum=2 --bool-fmt=t --num-output --quoted-output --separator=" " --form --scroll \
    --text=$"YAD settings for text editor\nThis settings only usefull when YAD built with GtkSourceView\n\n" \
    --field=$"Default theme for text-info dialog::CB" \
    --field=$"Show line numbers:CHK" \
    --field=$"Hightlight current line:CHK" \
    --field=$"Enable line marks mode:CHK" \
    --field=$"Color of first type marks::CLR" \
    --field=$"Color of second type marks::CLR" \
    --field=$"Right margin position::NUM" \
    --field=$"Highlight matching brackets:CHK" \
    --field=$"Use autoindentation:CHK" \
    --field=$"Smart Home/End behavior::CB" \
    --field=$"Use smart backspace:CHK" \
    --field=$"Tabulation width::NUM" \
    --field=$"Indentation width::NUM" \
    --field=$"Insert spaces instead of tabulation:CHK" \
    -- \
    "${themes%?}" "${SourceView[LineNum]}" "${SourceView[LineNum]}" \
    "${SourceView[LineNarks]}" "${SourceView[Mark1Color]}" \
    "${SourceView[Mark2Color]}" "${SourceView[RightMargin]}!-1..256" \
    "${SourceView[Brackets]}" "${SourceView[Indent]}" "${he_str%?}" \
    "${SourceView[SmartBS]}" "${SourceView[TabWidth]}" \
    "${SourceView[IndentWidth]}" "${SourceView[Spaces]}" > $res2 &

# main dialog
yad --title=$"YAD settings" --key=$KEY --width=600 --height=700 --image=preferences-other \
    --button=$"Load defaults!view-refresh:3" --button="yad-save:0" --button="yad-close:1" \
    --text=$"<span size='xx-large' weight='bold'>YAD settings editor</span>" --text-align=center \
    --notebook --stack --expand --tab=$"Main" --tab=$"Editor"

if [[ $? -eq 0 ]]; then
    eval r1=($(< $res1))
    eval r2=($(< $res2))

    # get theme name
    ti=$((${r2[0]} - 1))
    if [[ $ti -eq 0 ]]; then
        theme=
    else
        theme=$(yad-tools --show-themes | sed "${ti}q;d")
    fi

    # fix smart home-end
    smarthe=$((${r2[9]} - 1))

    mkdir -p $(basename $filename)
    cat <<EOF > $filename
[Common]
# Default width of dialog window
Width=${r1[0]}
# Default height of dialog window
Height=${r1[1]}
# Borders around dialog
Border=${r1[2]}
# Show remaining time and percentage in timeout progress bar
ShowRemain=${r1[3]}
# Combo-box in entry dialog is always editable
ComboAlwaysEditable=${r1[4]}
# Default terminal command (use %s for arguments placeholder)
Terminal=${r1[5]}
# Default open command (use %s for arguments placeholder)
OpenCommand=${r1[6]}
# Default date format (see strftime(3) for details)
DateFormat=${r1[7]}
# Default color for URI highlight in text-info dialog
URIColor=${r1[8]}
# Maximum number of tabs in notebook dialog
MaxTab=${r1[9]}
# Use large previews in file selection dialogs)
LargePreview=${r1[10]}
# Set width of search entry in search bar
SearchWidth=${r1[11]}
# Ignore unknown command-line options
IgnoreUnknownOptions=${r1[12]}
# Enable debug mode with information about deprecated features
Debug=${r1[13]}

[SourceView]
# Default color theme for text-info dialog
Theme=$theme
# Show line numbers
LineNum=${r2[1]}
# Highlight current line
LineHilite=${r2[2]}
# Enable line marks mode
LineMarks=${r2[3]}
# Default color for first type of text marks
Mark1Color=${r2[4]}
# Default color for second type of text marks
Mark2Color=${r2[5]}
# Show right margin at position (0 to disable)
RightMargin=${r2[6]}
# Highlight matching brackets
Brackets=${r2[7]}
# Use autoindent
Indent=${r2[8]}
# Smart Home/End behavior (0 - never, 1 - before, 2 - after, 3 - always)
HomeEnd=$smarthe
# Use smart backspace
SmartBS=${r2[10]}
# Default tabulation width
TabWidth=${r2[11]}
# Default indentation width
IndentWidth=${r2[12]}
# Insert spaces instead of tabs
Spaces=${r2[13]}
EOF
elif [[ $? -eq 3 ]]; then
    # Load default settings
    rm -f $filename
    yad --write-settings
fi
