<?php
/**
 * Insert class
 *
 * @author Lucas Dantas <lucas@caju.nu>
 * @package classes
 * @subpackage insert
 * @version 1.0
 * @since 2018/04/11 11:58
 */
Class InsertController{

	/**
	 * Defining the repository
	 *
	 * @access public
	 * @static
	 * @uses InsertRepositoryDBR::getInstance Defining the instance
	 * @return mixed
	 */
	public static function getRepository(){

		//Retornando
		return InsertRepositoryDBR::getInstance();

	}

	/**
	 * Main method
	 *
	 * @access public
	 * @static
	 * @param string $form Form
	 * @param array $fields Fields
	 * @param array $upload Upload file
	 * @uses Util::formatFields Formating the fields
	 * @uses Validation::form Validating the form
	 * @uses self::proposal Adding the proposal
	 * @uses self::contact Adding the contact
	 * @uses self::central Adding the central
	 * @uses self::order Adding the order
	 * @uses self::newsletter Adding the newsletter
	 */
	public static function main( $form = null, $fields = [], $upload = null ){

		//Formating
		$fields	= Util::formatFields( $fields );

		//Validating the fields
		Validation::form( $form, $fields, $upload );

		//Checking
		switch( $form ){

			//Proposal
			case 'proposal':

				//Adding
				self::proposal( $fields );

			break;

			//Contact
			case 'contact':

				//Adding
				self::contact( $fields );

			break;

			//Central
			case 'central':

				//Adding
				self::central( $fields );

			break;

			//Order
			case 'order':

				//Adding
				self::order( $fields );

			break;

			//Newsletter
			case 'newsletter':

				//Adding
				self::newsletter( $fields );

			break;

		}

	}

	/**
	 * Inserting the proposal
	 *
	 * @access public
	 * @static
	 * @param array $fields
	 * @uses self::getRepository Getting the repository
	 * @uses InsertRepositoryDBR->proposal Inserting the proposal
	 * @uses EmailController::send Sending
	 */
	public static function proposal( $fields = [] ){

		//Defining
		$fields[ 'mensagem' ]	= $fields[ 'mensagem' ] ?? 'Gostaria de mais informações do produto/serviço selecionado.';
		$fields[ 'telefone' ]	= $fields[ 'telefone' ] ?? null;

		//Inserting and defining
		$id	= self::getRepository()->proposal( $fields );

		//Sending
		EmailController::send( $id, DESTINATION_PROPOSAL );

		//Sending the e-mail
		new Email( $fields[ 'email' ] , 'Seu contato foi enviado', EmailTemplate::default() );

	}

	/**
	 * Inserting the contact
	 *
	 * @access public
	 * @static
	 * @param array $fields
	 * @uses self::getRepository Getting the repository
	 * @uses InsertRepositoryDBR->contact Inserting the contact
	 * @uses EmailController::send Sending
	 */
	public static function contact( $fields = [] ){

		//Inserting and defining
		$id	= self::getRepository()->contact( $fields );

		//Sending
		EmailController::send( $id, DESTINATION_CONTACT );

		//Sending the e-mail
		new Email( $fields[ 'email' ] , 'Seu contato foi enviado', EmailTemplate::default() );

	}

	/**
	 * Inserting the order
	 *
	 * @access public
	 * @static
	 * @param array $fields
	 * @uses self::getRepository Getting the repository
	 * @uses InsertRepositoryDBR->order Inserting the order
	 * @uses EmailController::send Sending
	 * @uses EmailTemplate::default Defining the default's template
	 */
	public static function order( $fields = [] ){

		//Inserting and defining
		$id	= self::getRepository()->order( $fields );

		//Sending
		EmailController::send( $id, DESTINATION_ORDER );

		//Sending the e-mail
		new Email( $fields[ 'email' ] , 'Seu contato foi enviado', EmailTemplate::default() );

	}

	/**
	 * Inserting the newsletter
	 *
	 * @access public
	 * @static
	 * @param array $fields
	 * @uses self::getRepository Getting the repository
	 * @uses CountController::byEmail Counting by e-mail
	 * @uses InsertRepositoryDBR->newsletter Inserting the newsletter
	 */
	public static function newsletter( $fields = [] ){

		//Checking
		if( CountController::byEmail( 'newsletter', $fields[ 'email' ] ) == 0 )
			//Inserting
			self::getRepository()->newsletter( $fields );

	}

	/**
	 * Inserting the central
	 *
	 * @access public
	 * @static
	 * @param array $fields
	 * @uses self::getRepository Getting the repository
	 * @uses SearchController::byIdByDestination Searching by your identifier and destination
	 * @uses InsertRepositoryDBR->central Inserting the central
	 * @uses EditController::statusByIdByDestination Editing the status by your identifier and destination
	 * @throws Exception
	 */
	public static function central( $fields = [] ){

		//Defining
		$object	= SearchController::byIdByDestination( $fields[ 'formulario' ], $fields[ 'destino' ] );

		//Checking
		if( md5( HASH . $object->id . $object->email ) != $fields[ 'token' ] )
			//Throwing the exception
			throw new Exception( 'Você não tem permissão para acessar essa página. Por favor, tente novamente mais tarde.' );
		else{

			//Inserting
			self::getRepository()->central( $fields[ 'formulario' ], $fields[ 'destino'] , $fields[ 'mensagem' ] );

			//Editing
			EditController::statusByIdByDestination( $fields[ 'formulario' ], $fields[ 'destino' ] );

		}

	}

}