<?php

namespace Hecate\Components;
use Hecate\Lodash;

class SettingsApi {

	protected $component              = null;
	protected $componentParts         = null;
	protected $componentDefaults      = null;
	protected $defaultComponentPartId = null;
	protected $data                   = null;
	protected $mergedData             = null;

	public function __construct( ComponentBase $component ) {
		$this->component              = $component;
		$this->componentParts         = $component->componentParts;
		$this->componentDefaults      = $this->getComponentDefaults();
		$this->defaultComponentPartId = $this->getDefaultComponentPartId();
		$this->data                   = $component->getData( 'data' );
        $test = 4;
	}


	public function getComponentDefaults() {
		return Lodash::get( $this->component->componentDefaults, 'data', array() );
	}

	public function getDesign( $path, $defaultValue = null, $options = array() ) {
		$mergedOptions = $this->getMergedOptions( $options );
		$absolutePath  = $this->getAbsoluteDesignPath( $path, $mergedOptions );
		return $this->getData( $absolutePath, $defaultValue, $mergedOptions );
	}

	public function getAttribute( $path, $defaultValue = null, $options = array() ) {
		$mergedOptions = $this->getMergedOptions( $options );
		$absolutePath  = $this->getAbsoluteAttributePath( $path, $mergedOptions );
		return $this->getData( $absolutePath, $defaultValue, $mergedOptions );
	}

	public function getData( $path, $defaultValue, $options ) {
		$fromRoot = $options['fromRoot'];

		if ( $fromRoot ) {
			$source = $this->data;
		} else {
			$source = $this->getMergedData();
		}

		return Lodash::get( $source, $path, $defaultValue );
	}

	public function getMergedData() {
		//todo create mergedData;
		return Lodash::merge( $this->componentDefaults, $this->data );
	}

	public function getAbsoluteDesignPath( $path, $options = array() ) {
		return $this->getAbsolutePath( $path, array_merge( $options, array( 'prefix' => 'design' ) ) );
	}

	public function getAbsoluteAttributePath( $path, $options = array() ) {
		return $this->getAbsolutePath( $path, array_merge( $options, array( 'prefix' => 'attribute' ) ) );
	}

	public function getAbsolutePath( $relativePath, $options = array() ) {
		$prefix        = $options['prefix'];
		$media         = $options['media'];
		$state         = $options['state'];
		$componentPart = $options['componentPart'];

		$paths = array( $prefix );

		$isDesign = $prefix === 'design';
		if ( $isDesign && $componentPart ) {
			$paths[] = "componentPart.{$componentPart}";
		}
		if ( $media !== 'desktop' ) {
			$paths[] = "media.{$media}";
		}

		if ( $isDesign && $state !== 'normal' ) {
			$paths[] = "state.{$state}";
		}
		if ( $relativePath ) {
			$paths[] = $relativePath;
		}
		$joinedPaths = implode( '.', $paths );
		return $joinedPaths;
	}

	public function getMergedOptions( $options = array() ) {
		$defaultOptions = array(
			'media'         => 'desktop',
			'state'         => 'normal',
			'fromRoot'      => false,
			'componentPart' => $this->defaultComponentPartId,
		);

		return array_merge( $defaultOptions, $options );
	}

	public function getDefaultComponentPartId() {

		$defaultComponentPartId = null;
		foreach ( $this->componentParts as $componentPart ) {
			if ( isset( $componentPart['default'] ) && $componentPart['default'] === true ) {
				$defaultComponentPartId = $componentPart['id'];
			}
		}

		return $defaultComponentPartId;
	}


}
