

import tools	 	from './tools.js';

export default class querySQL{

	__className		= "querySQL";
	
	type			= "select";
	
	columns			= new Array();
	
	table;

	sync			= true;


	values			= new Array();

	objects			= new Array();
	
	where			= new Array();
	
	groups 			= new Array();
 
	union 			= new Array();

	order 			= new Array();

	direction;

	limit;

	count;

	from;

	to;
	
	joins			= new Array();

	filter 			= false;

	filterAddress 	= "";
	
	
	
	constructor( query ) {
	
		if( query ) {
		
			var items = Object.entries( query );
			
			var queryString = "";

			for(var c = 0; c<items.length; c++){
				
				var currentQuery = items[c];
				
				var name	= tools.getClassNameByEntry( currentQuery);
				var value	= tools.getObjectByEntry( currentQuery);
				
				this[ name ] = value;
				
			}

		}
	
	}

	orderBy( order ) {

		this.order.push( order );

	}

	addUnion( unionQuery ) {

		this.union.push( unionQuery );

	}

	setFilterAddress( filter ) {

		this.filterAddress = filter;

	}

	setFilter( filterAddress ) {

		this.filter = filterAddress;

	}
	
	addJoin( join ) {

		if( !join.name ) {

			console.error( "join requires name", this, join );

		}
	
		this.joins.push( join );
	
	}
	
	addColumn( columnName ) {
	
		this.columns.push( columnName );
	
	}

	addGroup( columnName ) {

		var group = new Object();

		group.columnName = columnName;

		this.groups.push( group );


	}

	getWhereByColumnName( name ) {

		for (var i = 0; i < this.where.length; i++) {

			var where = this.where[i];

			if( where.columnName == name ) {

				return where.value;

			}

		}


		return false;

	}
	
	find( columnName, value, type = "=", quotation = true, or = false ) {
	
		var where = {};
		
		where.columnName 	= columnName;
		where.value			= value;
		where.type			= type;
		where.quotation		= quotation;
		where.or			= or;


		
		this.where.push( where );
	}
	
	getColumnByName( name ) {

		for (var i = 0; i < this.values.length; i++) {

			var column = this.values[i];

			if( column.name == name ) {

				return column;

			}

		}


		return false;

	}
	
	setColumn( columnName, value ) {
	
		if( typeof value == "boolean" ) {

			if( value ) {

				value = "1";

			} else {

				value = "0";

			}

		}

		var column 		= new Object();
		
		column.name 	= columnName;
		
		column.value	= value;
		
		this.values.push( column );
	
	}
	
	setValue( columnName, value ) {
	
		var column = {};

		if( typeof value == "boolean" ) {

			if( value ) {

				value = "true";

			} else {

				value = "false";

			}

		}


		column.name 	= columnName;
		column.value	= value;
		
		this.values.push( column );
	
	}
	
	set( columnName, value ) {
	
		var column = {};
		
		column.name 	= columnName;
		column.value	= value;
		
		this.values.push( column );
	
	}

	getColumnNames() {
	
		var columnNames = new Array();
		
		for(var c = 0; c<this.values.length;c++) {
		
			columnNames.push( this.values[c].name );
		
		}
		
		return columnNames.join(", ");
		
	}
	
	getValues() {
	
		var values = new Array();
		
		for(var c = 0; c<this.values.length;c++) {
		
			values.push( this.values[c].value );// todo : bug : 
		
		}
		
		return values;
	}

	extractColumns( object, queryName ) {

		var children = object.getChildren();

		this.addColumn( queryName + ".id" );
		
		for(var c = 0; c<children.length; c++) {
		
			var child = children[c];
			
			if( child.datatype ) {

				this.addColumn( queryName + "." + child.propertyName );

			}

			if( child.type == "table" ) {

				var name = child.propertyName;

				var columnName	= name + "_id";

				this.addColumn( queryName + "." + columnName );

			}



		}


	}
/* doesnt make sense
	addRows( rows ) {

		for (var i = 0; i < rows.length; i++) {

			this.addRow( rows[i] );

		}

	}
*/
	addRow( row ) {

		const entries = Object.entries( row );

		for (var i = 0; i < entries.length; i++) {

			var entry = entries[i];

			this.addColumn( entry[0] );

			this.setColumn(entry[0], entry[1]);

		}

	}
	
	addObject( object ) {

		if( this.objects.length == 0 ) {
		
			this.table = tools.getTableName( object );

			if( !this.table ) {
			
				this.table = tools.getTableName( object.parent );
				
				this.setColumn( object.propertyName, object.value );
				
			} else {


				if( object.user && object.user.id ) {

					var properties = object.getProperties();

					for( var c = 0;c<properties.length;c++ ) {
					
						var property = properties[c];

						if( property.value == "userplaceholder" && property.name != "user") {
								
							this.setColumn( property.name + "_id", object.user.id );

						}
						
					}

				}

				var children = object.getChildren();

				for( var c = 0; c < children.length; c++ ) {



					var child = children[c];

					//console.log(child.propertyName, child.type, child.extendsClass( "table" ));

					if( child.datatype ) {

						if( typeof child.constraints == "object" ) {

							if( child.constraints.includes("AUTOINCREMENT") ) {

								//console.log("skip this row, Dont overide AUTOINCEMENT");
								continue;

							}

						}

						// get Column name

						var columnName	= child.getColumnName();

						if( !columnName ) {

							columnName = child.propertyName;

						}

						//console.log("save column", child.propertyName, child.getExtends(), child.getColumnName() );

						this.setColumn( columnName , child.value );

					}

					// bug fixed.
					if( child.type == "table" && child.extendsTable() ) { // && child.extendsClass( "table" )?? bug

						var name 			= child.propertyName;

						var columnName		= name + "_id";

						var id				= child.id;

						//console.log("id", id);
						// ???
						if( id ) {
											
							this.setColumn( columnName, id );

						}
						
					} //else {

						//if( child.type == "table" ) {

							//console.log("what is so special about this", child);

						//}

					//}

					
				}
			
			}


			
			if(this.type.toLowerCase() == "update") {
			
				this.find( "id", object.id );
				
			}
			
		}

		//console.log("added object to query", this);
		
		this.objects.push( object );
	
	}
	
	queryUpdateChildren() {
	
		var objects = this.objects;

		var id = 0;
		
		for(var c = 0; c<objects.length; c++) {
		
			var object = objects[c];

			var values 	= new Array();
			var children 	= object.getChildren();

			for(var c = 0; c<children.length; c++) {
			
				var child = children[c];
				
				if(child.datatype) {
				
					values.push( child.value );
					
				}
			}
			
			values.push( object.id );

			
			
			//Console.log( "update children", values );
			
			//this.sql.run( values );

		}
	
	
	}
	
}
