<?php



use Idearia\Logger;



class WooOrder extends BaseObject {



	private $products = [];



	public function __construct() {

		parent::__construct("WooModel");

	}





	/* MAIN LOGIC */



	public function load($orderId) {



		Logger::debug( "Loading WooOrder({$orderId})" );



		if ($orderData = $this->model()->getOrder($orderId)) {

			$this->props($orderData);

			$this->isLoaded(true);

			Logger::info( "WooOrder loaded" );

			

			return true;

		} else {

			Logger::error( "WooOrder({$orderId}) failed to load" );



			return false;

		}

	}

	public function update($orderData = []) {

		

		Logger::debug( "Updating WooOrder..." );

		

		if (!$this->isLoaded()) {

			Logger::error( "Error: WooOrder is not loaded" );

			return false;

		}

		

		if ($orderId = $this->prop("id")) {

			if ($result = $this->model()->putOrder($orderId, $orderData)) {

				$this->props($orderData);

				Logger::info( "WooOrder updated" );



				return true;

			} else {

				Logger::error( "WooOrder update failed" );

				

				return false;

			}

		} else {

			Logger::error( "Error: WooOrder invalid ID" );

		}



		return false;

	}





	/* DATA LOGIC */



	public function loadProducts() {

		if ($lines = $this->prop("line_items")) {

			foreach ($lines as $line) {

				if ($productId = $line["product_id"]) {

					$product = new IntWooProduct();

					$product->load($productId);

					$product->setLine($line);



					$this->products[ $line["id"] ] = $product;

				}

			}

		}

	}





	/* PUBLIC SETTERS */





	/* PUBLIC GETTERS */



	public function getProducts($lineId = false) {

		if ($lineId) {

			return $this->products[$lineId] ?? false;

		} else {

			return $this->products;

		}

	}

	public function getBrands() {

		$brands = array_map(fn($product) => $product->getBrand(), $this->products);

		return array_unique($brands);

	}

	public function getDeliveryAddress() {

		

		$addressData = [

			$this->prop(["billing", "city"]), 

			$this->prop(["billing", "address_1"]), 

			$this->prop(["billing", "address_2"])

		];



		return implode(" ", array_filter($addressData, "trim"));

	}

	public function getPrefix() {

		switch ($this->prop("payment_method")) {

			case "dolyamepayment":

				return "D";

			case "cod":

				return "S";

			case "":

				return "F";

			default:

				return "N";

		}

	}

	public function getType() {

		switch ($this->detectType()) {

			case "SITE_BACKORDER":

				return BitrixConst::TYPE_SITE_BACKORDER;

			case "SITE_PARTNER":

				return BitrixConst::TYPE_SITE_PARTNER;

			default:

				return BitrixConst::TYPE_SITE;

		}

	}

	public function getPaymentMethod() {

		switch ($this->prop("payment_method")) {

			case "dolyamepayment":

				return BitrixConst::PAYMENT_DOLYAMI;

			case "cod":

			default:

				return BitrixConst::PAYMENT_CDEK;

		}

	}





	/* PRIVATE LOGIC */



	private function isOnBackorder() {

		foreach ($this->products as $product) {

			if ($product->isOnBackorder()) {

				return true;

			}

		}

		return false;

	}

	private function isPartner() {

		foreach ($this->products as $product) {

			if ($product->isPartner()) {

				return true;

			}

		}

		return false;

	}

	private function detectType() {

		if ($this->isPartner()) {

			return "SITE_PARTNER";

		}



		if ($this->isOnBackorder()) {

			return "SITE_BACKORDER";

		}



		return "SITE";

	}



}