#!/bin/bash

sleep 0.2
write_config_dir=$HOME/.config/write
write_config_file_path=$write_config_dir/write.conf
write_config_exists_out=$(cat $write_config_file_path &> /dev/null)
write_config_exists=$?
write_default_notes_dir=$HOME/Write-Write
write_default_note_num=1
write_default_editor=$EDITOR
write_default_note_extension=".md"
logseq_page_path="$HOME/Nextcloud/Notes/Logseq/pages/write-write.md"
note_num=$write_default_note_num
bold_on="setterm --bold on"
bold_off="setterm --bold off"

make_default_config() {
	mkdir -p $write_config_dir || exit 2

	echo "write_notes_dir=$write_default_notes_dir" > $write_config_file_path
	echo "note_num=$write_default_note_num" >> $write_config_file_path
	echo "write_note_extension=$write_default_note_extension" >> $write_config_file_path
	echo "write_editor=$write_default_editor" >> $write_config_file_path

	mkdir -p $write_default_notes_dir || exit 2
}

make_config_file() {
	mkdir -p $write_config_dir || exit 2

	echo "write_notes_dir=$write_notes_dir" > $write_config_file_path
	echo "note_num=$note_num" >> $write_config_file_path
	echo "write_note_extension=$write_note_extension" >> $write_config_file_path
	echo "write_editor=$write_editor" >> $write_config_file_path

	mkdir -p $write_notes_dir || exit 2
}

make_config_dialog() {
	$bold_on && echo "Would you like to change the default notes directory? $write_default_notes_dir"
	echo "[y,N]" && $bold_off
	read change_notes_dir
	if [[ $change_notes_dir = "y" || $change_notes_dir = "Y" ]]
	then
		$bold_on && echo "Enter absolute directory of stored files e.g. /home/$(whoami)/notes" && $bold_off
		read write_notes_dir_input
		write_notes_dir=$write_notes_dir_input
		make_config_file
	else
		write_notes_dir=$write_default_notes_dir
		make_config_file
	fi
	mkdir -p $write_notes_dir || exit 2

	$bold_on && echo "Would you like to change the current editor ($EDITOR)?"
	echo "[y,N]" && $bold_off
	read change_editor
	if [[ $change_editor = "y" || $change_editor = "Y" ]]
	then
		$bold_on && echo "Enter the command used to run the editor (e.g. 'nano', 'vi', 'micro')" && $bold_off
		read write_editor_input
		write_editor=$write_editor_input
		make_config_file
	else
		write_editor=$write_default_editor
		make_config_file
	fi


	$bold_on && echo "Would you like to change the current file extension for the notes ($write_default_note_extension)?"
	echo "[y,N]" && $bold_off
	read change_note_extension
	if [[ $change_note_extension = "y" || $change_note_extension = "Y" ]]
	then
		$bold_on && echo "Enter the file extension desired (e.g. '.md', '.txt')" && $bold_off
		read write_note_extension_input
		write_note_extension="$write_note_extension_input"
		make_config_file
	else
		write_note_extension=$write_default_note_extension
		make_config_file
	fi


	source $write_config_file_path

}

append_note_logseq() {
    echo "" >> $logseq_page_path
    echo "# $note_num$write_note_extension" >> $logseq_page_path
#    echo "      -" >> $logseq_page_path
    cat $write_notes_dir/$note_num$write_note_extension >> $logseq_page_path
    echo "" >> $logseq_page_path
}

check_note_diff() {
    diff_state=$(diff $write_notes_dir/$note_num$write_note_extension $write_notes_dir/$note_num$write_note_extension.previous &> /dev/null && echo $?)
}

update_note_num() {
    note_exists=$(cat $write_notes_dir/$note_num$write_note_extension &> /dev/null && echo $?)
	if [[ $note_exists = 0 ]]
	then
		note_num=$(($note_num + 1))
	fi
	make_config_file
}

new_note() {
	$write_editor $write_notes_dir/$note_num$write_note_extension
    note_exists=$(cat $write_notes_dir/$note_num$write_note_extension &> /dev/null && echo $?)
    if [[ $note_exists = 0 ]]
    then
        append_note_logseq
    fi
    update_note_num
}

open_note(){
    cp $write_notes_dir/$note_num$write_note_extension $write_notes_dir/$note_num$write_note_extension.previous
	$write_editor $write_notes_dir/$note_num$write_note_extension
    check_note_diff
    if [[ $diff_state = 1 ]]
    then
        replace_note_logseq
    fi
}

get_entries() {
    ls -1 $write_notes_dir | while read note
    do
        $bold_on && echo "$note" && $bold_off
        cat -n $write_notes_dir/$note | while read line
        do
            line_num=$(echo $line | cut -d ' ' -f1)
            new_line=$(echo $line | sed "s/$line_num //g")
#            line_num=$(echo "$line" | cut --delimiter=' ' -f1)
            if [[ $line_num -lt 5 ]]
            then
#                echo $line_num
                echo "$new_line"
#                echo $line | sed "s/$line_num //"
            fi
        done
        echo
        echo
    done
#	for i in $notes_list
#	do
#		echo "$i"
#       sleep 2
#		for line in $(cat -n $write_notes_dir/$i)
#		do
#			if [[ $(echo $line | cut --delimiter=' ' -f1) < 5 ]]
#			then
#				echo $line
#			fi
#		done
#	done	
}

if [[ $write_config_exists = 1 ]]  # 1 means not true in this case (exit code)
then
	make_default_config
	echo "write-write Copyright (C) 2021 Adriel Sand
This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See https://gitlab.com/thebiblelover7/npad for more details."
	$bold_on
	echo "Would you like to change the defaults?"
	echo "[y,N]" && $bold_off
	read change_defaults
	if [[ $change_defaults = "y" || $change_defaults = "Y" ]]
	then
		make_config_dialog
	else
		make_default_config
	fi

	source $write_config_file_path || exit 2	

else
	source $write_config_file_path || exit 2
fi

case $1 in
	'list' | '-l' | '--list')
		get_entries
		;;
	*)
		note_num=$1
		note_exists=$(cat $write_notes_dir/$note_num$write_note_extension &> /dev/null && echo $?)
		if [[ $note_exists = 0 ]]
		then
			open_note
		else
			source $write_config_file_path || exit 2
			new_note
		fi
		;;
esac
