<?php
/*
 * $License$
 */

/**
 * @package Modules\UIKit
 */
class UIKitSampleFactory extends Factory {
	protected $table = 'ui_kit';

	/**
	 * @param $name
	 * @param null $parent
	 * @return array|null
	 */
	function _getFactoryOptions( $name, $parent = null ) {

		$retval = null;
		switch ( $name ) {
			case 'combo_box':
				$retval = [
						10 => TTi18n::gettext( 'Option 1' ),
						20 => TTi18n::gettext( 'Option 2' ),
						30 => TTi18n::gettext( 'Option 3' ),
						40 => TTi18n::gettext( 'Option 4' ),
				];
				break;
			case 'columns':
				$retval = [
						'-1010-text_input'       => TTi18n::gettext( 'Name' ),
						'-1020-tag'              => TTi18n::gettext( 'Tags' ),
						'-1030-combo_box_parent' => TTi18n::gettext( 'Combo Box Parent' ),
						'-1030-combo_box_child'  => TTi18n::gettext( 'Combo Box Child' ),
						'-1040-date'             => TTi18n::gettext( 'Date' ),
						'-1060-time'             => TTi18n::gettext( 'Time' ),
						'-1070-checkbox'         => TTi18n::gettext( 'Checkbox' ),
						'-1080-numeric_input'    => TTi18n::gettext( 'Numeric' ),

						'-2000-created_by'   => TTi18n::gettext( 'Created By' ),
						'-2010-created_date' => TTi18n::gettext( 'Created Date' ),
						'-2020-updated_by'   => TTi18n::gettext( 'Updated By' ),
						'-2030-updated_date' => TTi18n::gettext( 'Updated Date' ),
				];

				$retval = $this->getCustomFieldsColumns( $retval, null );

				break;
			case 'list_columns':
				$retval = Misc::arrayIntersectByKey( $this->getOptions( 'default_display_columns' ), Misc::trimSortPrefix( $this->getOptions( 'columns' ) ) );
				break;
			case 'default_display_columns': //Columns that are displayed by default.
				$retval = [
						'text_input',
						'tag',
						'combo_box_parent',
						'combo_box_child',
						'date',
						'time',
						'checkbox',
						'numeric_input',

				];
				break;
		}

		return $retval;
	}

	/**
	 * @param $data
	 * @return array
	 */
	function _getVariableToFunctionMap( $data ) {
		$variable_function_map = [
				'id'                 => 'ID',
				'company_id'         => 'Company',
				'text_input'         => 'TextInput',
				'password_input'     => 'PasswordInput',
				'numeric_input'      => 'NumericInput',
				'textarea'           => 'Textarea',
				'checkbox'           => 'Checkbox',
				'wysiwg_text'        => 'WYSIWGText',
				'tag'                => 'Tag',
				'combo_box'          => 'ComboBox',
				'combo_box_parent'   => 'ComboBoxParent',
				'combo_box_child'    => 'ComboBoxChild',
				'awesome_box_multi'  => 'AwesomeBoxMulti',
				'awesome_box_single' => 'AwesomeBoxSingle',
				'date'               => 'Date',
				'date_range'         => 'DateRange',
				'time'               => 'Time',
				'color'              => 'Color',
				'formula_builder'    => 'FormulaBuilder',
				'deleted'            => 'Deleted',
		];

		return $variable_function_map;
	}

	/**
	 * @return bool|mixed
	 */
	function getCompany() {
		return $this->getGenericDataValue( 'company_id' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setCompany( $value ) {
		$value = TTUUID::castUUID( $value );

		return $this->setGenericDataValue( 'company_id', $value );
	}

	/**
	 * @return string
	 */
	function getTextInput() {
		return $this->getGenericDataValue( 'text_input' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setTextInput( $value ) {
		$value = trim( $value );

		return $this->setGenericDataValue( 'text_input', $value );
	}

	/**
	 * @return string
	 */
	function getPasswordInput() {
		return $this->getGenericDataValue( 'password_input' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setPasswordInput( $value ) {
		$password = trim( $value );

		//Check to see if the password is hashed and being passed back into itself from the LogDetailFactory or UIKitSample
		if ( strlen( $password ) > 100 && strpos( $password, ':' ) !== false ) {
			Debug::Text( 'Password is hashed, ignoring: ' . $password, __FILE__, __LINE__, __METHOD__, 10 );

			return false;
		}

		return $this->setGenericDataValue( 'password_input', TTPassword::encryptPassword( $password, $this->getCompany() ) );
	}

	/**
	 * @return string
	 */
	function getNumericInput() {
		return (float)$this->getGenericDataValue( 'numeric_input' ); //Needs to return float so TTi18n::NumberFormat() can always handle it properly.
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setNumericInput( $value ) {
		//Pull out only digits and periods.
		$value = $this->Validator->stripNonFloat( $value );

		return $this->setGenericDataValue( 'numeric_input', $value );
	}

	/**
	 * @return string
	 */
	function getTag() {
		return $this->getGenericDataValue( 'tag' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setTag( $value ) {
		$value = trim( $value );

		return $this->setGenericDataValue( 'tag', $value );
	}

	/**
	 * @return string
	 */
	function getTextarea() {
		return $this->getGenericDataValue( 'textarea' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setTextarea( $value ) {
		$value = trim( $value );

		return $this->setGenericDataValue( 'textarea', $value );
	}

	/**
	 * @return string
	 */
	function getWYSIWGText() {
		return $this->getGenericDataValue( 'wysiwg_text' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setWYSIWGText( $value ) {
		$value = trim( $value );

		return $this->setGenericDataValue( 'wysiwg_text', $value );
	}

	/**
	 * @param bool $value
	 * @return bool
	 */
	function setCheckbox( $value ) {
		return $this->setGenericDataValue( 'checkbox', $this->toBool( $value ) );
	}

	/**
	 * @return bool
	 */
	function getCheckbox() {
		return $this->fromBool( $this->getGenericDataValue( 'checkbox' ) );
	}

	/**
	 * @return bool|int
	 */
	function getComboBox() {
		return $this->getGenericDataValue( 'combo_box' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setComboBox( $value ) {
		$value = (int)trim( $value );

		return $this->setGenericDataValue( 'combo_box', $value );
	}

	/**
	 * @return bool|int
	 */
	function getComboBoxParent() {
		return $this->getGenericDataValue( 'combo_box_parent' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setComboBoxParent( $value ) {
		$value = trim( $value );

		return $this->setGenericDataValue( 'combo_box_parent', $value );
	}

	/**
	 * @return bool|int
	 */
	function getComboBoxChild() {
		return $this->getGenericDataValue( 'combo_box_child' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setComboBoxChild( $value ) {
		$value = trim( $value );

		return $this->setGenericDataValue( 'combo_box_child', $value );
	}

	/**
	 * @return array
	 */
	function getAwesomeBoxMulti() {
		return $this->getGenericJSONDataValue( 'awesome_box_multi' );
	}

	/**
	 * @param array $value records.
	 * @return bool
	 */
	function setAwesomeBoxMulti( $value ) {

		return $this->setGenericJSONDataValue( 'awesome_box_multi', $value );
	}

	/**
	 * @return array
	 */
	function getAwesomeBoxSingle() {
		return $this->getGenericDataValue( 'awesome_box_single' );
	}

	/**
	 * @param array $value records.
	 * @return bool
	 */
	function setAwesomeBoxSingle( $value ) {
		return $this->setGenericDataValue( 'awesome_box_single', $value );
	}


	/**
	 * @param bool $raw
	 * @return bool|mixed
	 */
	function getDate( $raw = false ) {
		$value = $this->getGenericDataValue( 'date' );
		if ( $value !== false ) {
			if ( $raw === true ) {
				return $value;
			} else {
				return TTDate::strtotime( $value );
			}
		}

		return false;
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setDate( $value ) {
		return $this->setGenericDataValue( 'date', TTDate::getISODateStamp( $value ) );
	}

	/**
	 * @return array
	 */
	function getDateRange() {
		return $this->getGenericJSONDataValue( 'date_range' );
	}

	/**
	 * @param array $value records.
	 * @return bool
	 */
	function setDateRange( $value ) {
		return $this->setGenericJSONDataValue( 'date_range', $value );
	}

	/**
	 * @param bool $raw
	 * @return bool|int
	 */
	function getTime( $raw = false ) {
		$value = $this->getGenericDataValue( 'time' );
		if ( $value !== false ) {
			if ( $raw === true ) {
				return $value;
			} else {
				return TTDate::strtotime( $value );
			}
		}

		return false;
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setTime( $value ) {
		$value = ( !is_int( $value ) && $value !== null ) ? trim( $value ) : $value;//Dont trim integer values, as it changes them to strings.

		return $this->setGenericDataValue( 'time', $value );
	}

	/**
	 * @return bool|mixed
	 */
	function getColor() {
		return $this->getGenericJSONDataValue( 'color' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setColor( $value ) {
		$value = trim( $value );

		return $this->setGenericJSONDataValue( 'color', $value );
	}

	/**
	 * @return bool|mixed
	 */
	function getFormulaBuilder() {
		return $this->getGenericJSONDataValue( 'formula_builder' );
	}

	/**
	 * @param $value
	 * @return bool
	 */
	function setFormulaBuilder( $value ) {
		$value = trim( $value );

		return $this->setGenericJSONDataValue( 'formula_builder', $value );
	}

	/**
	 * @param bool $ignore_warning
	 * @return bool
	 */
	function Validate( $ignore_warning = true ) {
		$this->validateCustomFields( $this->getCompany() );
		return true;
	}

	/**
	 * @return bool
	 */
	function preSave() {
		return true;
	}

	/**
	 * @return bool
	 */
	function postSave() {
		return true;
	}

	/**
	 * @param $data
	 * @return bool
	 */
	function setObjectFromArray( $data ) {
		if ( is_array( $data ) ) {
			$data = $this->parseCustomFieldsFromArray( $data );
			$variable_function_map = $this->getVariableToFunctionMap();
			foreach ( $variable_function_map as $key => $function ) {
				if ( isset( $data[$key] ) ) {

					$function = 'set' . $function;
					switch ( $key ) {
						case 'time':
						case 'date':
							if ( method_exists( $this, $function ) ) {
								$this->$function( TTDate::parseDateTime( $data[$key] ) );
							}
							break;
						case 'numeric_input':
							$this->$function( TTi18n::parseFloat( $data[$key] ) );
							break;
						default:
							if ( method_exists( $this, $function ) ) {
								$this->$function( $data[$key] );
							}
							break;
					}
				}
			}

			$this->setCreatedAndUpdatedColumns( $data );

			return true;
		}

		return false;
	}

	/**
	 * @param null $include_columns
	 * @return array
	 */
	function getObjectAsArray( $include_columns = null ) {
		$data = [];
		$variable_function_map = $this->getVariableToFunctionMap();
		if ( is_array( $variable_function_map ) ) {
			foreach ( $variable_function_map as $variable => $function_stub ) {
				if ( $include_columns == null || ( isset( $include_columns[$variable] ) && $include_columns[$variable] == true ) ) {

					$function = 'get' . $function_stub;
					switch ( $variable ) {
						case 'date':
							if ( method_exists( $this, $function ) ) {
								$data[ $variable ] = TTDate::getAPIDate( 'DATE', $this->$function() );
							}
							break;
						case 'time':
							$data[$variable] = ( defined( 'TIMETREX_API' ) ) ? TTDate::getAPIDate( 'TIME', TTDate::strtotime( $this->$function() ) ) : $this->$function();
							break;
						case 'numeric_input':
							$data[$variable] = Misc::removeTrailingZeros( $this->$function(), 2 );
							break;
						case 'password_input': //Must not be returned to the API ever due to security risks. Replicating that in this UIkit
							break;
						default:
							if ( method_exists( $this, $function ) ) {
								$data[$variable] = $this->$function();
							}
							break;
					}
				}
			}
			$this->getCreatedAndUpdatedColumns( $data, $include_columns );
			$data = $this->getCustomFields( $this->getCompany(), $data, $include_columns );
		}

		return $data;
	}

	/**
	 * @param $log_action
	 * @return bool
	 */
	function addLog( $log_action ) {
		return TTLog::addEntry( $this->getId(), $log_action, TTi18n::getText( 'UI Kit Sample' ), null, $this->getTable(), $this );
	}
}

?>