#!/bin/bash

npad_config_dir=$HOME/.config/npad
npad_config_file_path=$npad_config_dir/npad.conf
npad_config_exists_out=$(cat $npad_config_file_path &> /dev/null)
npad_config_exists=$?
npad_default_notes_dir=$HOME/Npad
npad_default_note_num=1
npad_default_editor=$EDITOR
npad_default_note_extension=".md"
note_num=$npad_default_note_num
bold_on="setterm --bold on"
bold_off="setterm --bold off"

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

	echo "npad_notes_dir=$npad_default_notes_dir" > $npad_config_file_path
	echo "note_num=$npad_default_note_num" >> $npad_config_file_path
	echo "npad_note_extension=$npad_default_note_extension" >> $npad_config_file_path
	echo "npad_editor=$npad_default_editor" >> $npad_config_file_path

	mkdir -p $npad_default_notes_dir || exit 2
}

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

	echo "npad_notes_dir=$npad_notes_dir" > $npad_config_file_path
	echo "note_num=$note_num" >> $npad_config_file_path
	echo "npad_note_extension=$npad_note_extension" >> $npad_config_file_path
	echo "npad_editor=$npad_editor" >> $npad_config_file_path

	mkdir -p $npad_notes_dir || exit 2
}

make_config_dialog() {
	$bold_on && echo "Would you like to change the default notes directory? $npad_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 npad_notes_dir_input
		npad_notes_dir=$npad_notes_dir_input
		make_config_file
	else
		npad_notes_dir=$npad_default_notes_dir
		make_config_file
	fi
	mkdir -p $npad_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 npad_editor_input
		npad_editor=$npad_editor_input
		make_config_file
	else
		npad_editor=$npad_default_editor
		make_config_file
	fi


	$bold_on && echo "Would you like to change the current file extension for the notes ($npad_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 npad_note_extension_input
		npad_note_extension="$npad_note_extension_input"
		make_config_file
	else
		npad_note_extension=$npad_default_note_extension
		make_config_file
	fi


	source $npad_config_file_path

}

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

new_note() {
	$npad_editor $npad_notes_dir/$note_num$npad_note_extension
	update_note_num
}

open_note(){
	$npad_editor $npad_notes_dir/$note_num$npad_note_extension
}

get_entries() {
    ls -1 $npad_notes_dir | while read note
    do
        $bold_on && echo "$note" && $bold_off
        cat -n $npad_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 $npad_notes_dir/$i)
#		do
#			if [[ $(echo $line | cut --delimiter=' ' -f1) < 5 ]]
#			then
#				echo $line
#			fi
#		done
#	done	
}

if [[ $npad_config_exists = 1 ]]  # 1 means not true in this case (exit code)
then
	make_default_config
	echo "npad 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 $npad_config_file_path || exit 2	

else
	source $npad_config_file_path || exit 2
fi

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