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

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

class PaymentSubscription implements ISubscription {

	protected $_application, $_subscription;

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

	public function getSubscription() {
		return $this->_subscription;
	}

	public function paymentSubscriptionId() {
		return $this->_subscription->getId();
	}

	public function paymentSubscriptionNumber() {
		return $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'number' );
	}

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

		return $plan;
	}

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

	public function paymentSubscriptionStatus() {
		$statuses = $this->_application->PaymentGateway_Subscription_statuses();
		$status   = $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'status' );
		return isset( $statuses[ $status ] ) ? array( $statuses[ $status ]['label'], $statuses[ $status ]['color'] ) : $status;
	}

	public function paymentSubscriptionTotalHtml() {
		$data  = $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'data' );
		$total = $data['price'];
		if ( ! empty( $data['tax'] ) ) {
			$total += $total * $data['tax'] / 100;
		}
		return $this->_application->System_Currency_format( $total, $data['currency'] );
	}

	public function paymentSubscriptionNextPaymentHtml() {
		if ( $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'status' ) !== 'active' ) {
			return;
		}

		$next_payment_at = $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'next_payment_at' );
		if ( empty( $next_payment_at )
			|| $next_payment_at <= time()
		) {
			return;
		}

		return $this->_application->System_Date_datetime( $next_payment_at );
	}

	public function paymentSubscriptionAdminUrl() {
		return get_edit_post_link( $this->_subscription->getId() );
	}

	public function paymentSubscriptionUserId() {
		return $this->_subscription->getAuthorId();
	}

	public function paymentSubscriptionIsActive() {
		return $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'status' ) === 'active';
	}

	public function paymentSubscriptionEntityId() {
		return $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'parent_entity_id' );
	}

	public function paymentSubscriptionEntityType() {
		return $this->_subscription->getSingleFieldValue( 'paymentgateway_subscription', 'parent_entity_type' );
	}

	public function paymentSubscriptionPlan() {
		if ( ! $plan = $this->_getPlan() ) {
			throw new RuntimeException( 'No plan found for subscription: ' . $this->_subscription->getId() );
		}
		return new PaymentPlan( $plan );
	}
}
