#!/bin/sh

: "${YTFZF_GUI_CSS:=$YTFZF_CONFIG_DIR/interface-gui.css}"

display_text_gui () {
    echo "$*" > "${session_temp_dir}/data.gui"
    MAIN_DIALOG="
<window>
    <edit>
	<width>500</width>
	<height>500</height>
	<input file>${session_temp_dir}/data.gui</input>
    </edit>
</window>" gtkdialog
    rm -f "${session_temp_dir}/data.gui"
}

info_wait_prompt_gui () {
    :
}

gui_dialog () {
    css_file="${session_temp_dir}/gtk.css"
    if [ -f "$YTFZF_GUI_CSS" ]; then
        css_file="$YTFZF_GUI_CSS"
    else
        : > "$css_file"
    fi
    export MAIN_DIALOG="<window><vbox scrollable=\"true\" vscrollbar-policy=\"0\">"
    download_thumbnails $(get_missing_thumbnails)
    while read -r line; do
        url="${line##*|}"
        _correct_json="$(jq -r --arg url "$url" '[.[]|select(.url==$url)]|unique_by(.ID)[0]' < "$video_json_file")"
        id="$(get_video_json_attr "ID")"
        title="$(get_video_json_attr "title")"
        channel="$(get_video_json_attr "channel")"
        views="$(get_video_json_attr "views" | add_commas)"
        date="$(get_video_json_attr "date")"
        scraper="$(get_video_json_attr "scraper")"
        duration="$(get_video_json_attr "duration")"
        description="$(get_video_json_attr "description" | sed 's/\\n/\n/g')"

        unset IFS

        for path in "${YTFZF_CUSTOM_THUMBNAILS_DIR}/$id.jpg" "${thumb_dir}/${id}.jpg"  "${YTFZF_CUSTOM_THUMBNAILS_DIR}/YTFZF:DEFAULT.jpg"; do
            thumb_path="$path"
            [ -f "${thumb_path}" ] && break
        done

        export MAIN_DIALOG="$MAIN_DIALOG
        <hbox>
            <vbox>
                <text name=\"TitleText\">
                    <label>$title (@$channel)</label>
                </text>
                <text name=\"ViewsText\">
                    <label>$views views</label>
                </text>
                <text name=\"DateText\">
                    <label>uploaded: $date</label>
                </text>
                <text name=\"DurationText\">
                    <label>duration: $duration</label>
                </text>
                <button name=\"UrlButton\">
                    <label>$url</label>
                </button>
            </vbox>
            <pixmap name=\"Thumbnail\">
                <width>400</width>
                <input file>$thumb_path</input>
            </pixmap>
        </hbox>"
    done
    export MAIN_DIALOG="$MAIN_DIALOG<width>750</width><height>500</height></vbox></window>"
    MAIN_DIALOG=$MAIN_DIALOG gtkdialog --styles="$css_file"
}

interface_gui () {
    video_json_file="$1"
    selected_id_file="$2"
    command_exists "gtkdialog" || die 3 "gtkdialog (https://github.com/puppylinux-woof-CE/gtkdialog) is required for the gui interface\n"
	jq -r '.[]|"\(.title)\t|\(.channel)\t|\(.duration)\t|\(.views)\t|\(.date)\t|\(.url)"' < "$video_json_file" |
        sort_video_data_fn | gui_dialog | sed -n -e 's/EXIT="\(.*\)"/\1/p' -e 's/[[:space:]]*//g' > "$selected_id_file"
    while read -r line; do
        [ "$line" = "abort" ] && exit 0
    done < "$selected_id_file"
}

on_opt_parse_i_gui_help (){
    print_info "YTFZF_GUI_CSS is a file for custom css for the window\nselectors:\n#TitleText\n#ViewsText\n#DateText\n#DurationText\n#UrlButton\n#Thumbnail\n"
    exit 0
}

search_prompt_menu_gui () {
    vars=$(MAIN_DIALOG="
    <window>
	<vbox>
	    <entry>
		<default>Search...</default>
		<variable>search</variable>
	    </entry>
	    <button>
		<label>Search!</label>
	    </button>
	</vbox>
    </window>" gtkdialog)

    while read -r line; do
	case "$line" in
	    search*)
		_search=$(printf "%s" "$line" | tr -d '"')
		_search="${_search#*=}" ;;
	    EXIT*) break ;;
	esac
    done <<EOF
    $vars
EOF
}
