<?php
namespace SabaiApps\Directories\Component\PaymentGateway;

use SabaiApps\Directories\Application;
use SabaiApps\Directories\Component\Payment\IOrder;
use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\PaymentGateway\Model\Plan;
use SabaiApps\Directories\Exception\RuntimeException;

class PaymentOrder implements IOrder {

	protected $_application, $_order;

	public function __construct( Application $application, IEntity $order ) {
		$this->_application = $application;
		$this->_order       = $order;
	}

	/**
	 * @return IEntity
	 */
	public function getOrder() {
		return $this->_order;
	}

	public function setOrder( IEntity $order ) {
		$this->_order = $order;
		return $this;
	}

	public function paymentOrderId() {
		return $this->_order->getId();
	}

	public function paymentOrderNumber() {
		return $this->_order->getSingleFieldValue( 'paymentgateway_order', 'number' );
	}

	public function paymentOrderName() {
		return ( $plan = $this->_getPlan() ) ? $plan->getTitle() : null;
	}

	/**
	 * @return Plan|null
	 */
	protected function _getPlan() {
		if ( ( ! $plan_id = $this->_order->getSingleFieldValue( 'paymentgateway_order', 'plan_id' ) )
			|| ( ! $plan = $this->_application->Entity_Entity( 'post', $plan_id ) )
			|| $plan->getBundleType() !== 'paymentgateway_plan'
		) {
			return;
		}

		return $plan;
	}

	public function paymentOrderAction() {
		return $this->_order->getSingleFieldValue( 'paymentgateway_order', 'action' );
	}

	public function paymentOrderAdminUrl( $separator = '&amp;' ) {
		return $this->_application->Entity_AdminUrl( $this->_order, $separator );
	}

	public function paymentOrderStatus() {
		if ( ! $status = $this->_order->getSingleFieldValue( 'paymentgateway_order', 'status' ) ) {
			return;
		}

		$statuses = $this->_application->PaymentGateway_Order_statuses();
		if ( ! isset( $statuses[ $status ] ) ) {
			return;
		}

		return array(
			$statuses[ $status ]['label'],
			$statuses[ $status ]['color'],
		);
	}

	public function paymentOrderTotalHtml() {
		return $this->_application->PaymentGateway_Order_totalAmount( $this->_order, true, true );
	}

	public function paymentOrderTimestamp() {
		return $this->_order->getTimestamp();
	}

	public function paymentOrderEntityId() {
		return $this->_order->getSingleFieldValue( 'paymentgateway_order', 'parent_entity_id' );
	}

	public function paymentOrderEntityType() {
		return $this->_order->getSingleFieldValue( 'paymentgateway_order', 'parent_entity_type' );
	}

	public function paymentOrderWasItemDeactivated() {
		return ( $data = $this->_order->getSingleFieldValue( 'paymentgateway_order', 'data' ) )
			&& ! empty( $data['was_deactivated'] );
	}

	/**
	 * @return PaymentPlan
	 * @throws RuntimeException
	 */
	public function paymentOrderPlan() {
		if ( ! $plan = $this->_getPlan() ) {
			throw new RuntimeException( 'No plan found for order: ' . $this->_order->getId() );
		}
		return new PaymentPlan( $plan );
	}

	public function paymentOrderUserId() {
		return $this->_order->getAuthorId();
	}

	public function paymentOrderIsPendingPayment() {
		return $this->_order->getSingleFieldValue( 'paymentgateway_order', 'status' ) === 'pending_payment';
	}

	public function paymentOrderIsComplete() {
		return $this->_order->getSingleFieldValue( 'paymentgateway_order', 'status' ) === 'completed';
	}
}
