#include "externals.hh"
#include "filewatcher.hh"
#include "utilities.hh"


/*= T_FilesWatcher ===========================================================*/

T_FilesWatcher::T_FilesWatcher( )
	: fd( inotify_init1( O_NONBLOCK ) )
{ }

T_FilesWatcher::T_FilesWatcher( T_FilesWatcher&& other ) noexcept
	: fd( 0 ) , watched( std::move( other.watched ) )
{
	std::swap( fd , other.fd );
	other.watched.clear( );
	for ( T_WatchedFiles* wf : watched ) {
		if ( wf ) {
			wf->watcher = this;
		}
	}
}

T_FilesWatcher::~T_FilesWatcher( )
{
	if ( fd ) {
		close( fd );
	}
	for ( T_WatchedFiles* wf : watched ) {
		if ( wf ) {
			wf->watcher = nullptr;
		}
	}
}

void T_FilesWatcher::check( )
{
	for ( T_WatchedFiles* wf : watched ) {
		if ( wf ) {
			wf->triggered = false;
		}
	}

	inotify_event ie;
	while ( read( fd , &ie , sizeof( ie ) ) == sizeof( ie ) ) {
		if ( ( ie.mask & ( IN_CLOSE_WRITE | IN_DELETE_SELF ) ) == 0 ) {
			continue;
		}

		for ( T_WatchedFiles* wf : watched ) {
			if ( !wf || wf->triggered ) {
				continue;
			}
			auto const& idl( wf->identifiers );
			if ( find( idl , ie.wd ) != idl.end( ) ) {
				wf->triggered = true;
				wf->callback( );
			}
		}
	}
}

/*= T_WatchedFiles ===========================================================*/

T_WatchedFiles::T_WatchedFiles( T_WatchedFiles&& other ) noexcept
	: watcher( other.watcher ) , callback( other.callback ) ,
		triggered( other.triggered ) ,
		identifiers( std::move( other.identifiers ) )
{
	if ( watcher ) {
		other.watcher = nullptr;
		*( find( watcher->watched , &other ) ) = this;
	}
}

T_WatchedFiles::T_WatchedFiles(
		__rw__	T_FilesWatcher& watcher ,
		__rd__	const F_OnFileChanges callback )
	: watcher( &watcher ) , callback( callback ) , triggered( false )
{
	watcher.watched.push_back( this );
}

T_WatchedFiles::~T_WatchedFiles( )
{
	clear( );
	if ( watcher ) {
		watcher->watched.erase( find( watcher->watched , this ) );
	}
}

void T_WatchedFiles::clear( )
{
	if ( watcher ) {
		const auto fd( watcher->fd );
		for ( int wd : identifiers ) {
			inotify_rm_watch( fd , wd );
		}
	}
	identifiers.clear( );
}

bool T_WatchedFiles::watch(
		__rd__	std::string const& file )
{
	static constexpr auto inFlags( IN_CLOSE_WRITE | IN_DELETE_SELF );
	if ( watcher ) {
		const auto wd( inotify_add_watch( watcher->fd ,
					file.c_str( ) , inFlags ) );
		if ( wd == -1 ) {
			return false;
		}
		if ( find( identifiers , wd ) == identifiers.end( ) ) {
			identifiers.push_back( wd );
		}
		return true;
	}
	return false;
}