<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

use App\Models\Supplier;
use App\Models\Order;

class Supply extends Controller
{
	// Accepts a site name, service id, and integer/array of supplier rows to
	// create

	public static function fetch($site) {
		return Http::post(config("services.$site.link"), ['key' =>
			config("services.$site.key"), 'action' => 'services']);
	}

	public static function smmkings($service_id, $id) {
		$services = Http::post(config("services.smmkings.link"), ['key' =>
			config("services.smmkings.key"), 'action' => 'services'])->json();

		foreach ($services as $service) {
			if ($service['service'] == $id){
				$s = new Supplier;
				$s->service_id = $service_id;
				$s->supplier = 'smmkings';
				$s->supplier_id = (int) $service['service'];
				$s->supplier_name = $service['name'];
				$s->min = (int) $service['min'];
				$s->max = (int) $service['max'];
				$s->cost = $service['rate'] * 100;
				$s->save();
				return $s;
			} 
		}
		
	}

	public static function smmworld($service_id, $id) {
		$services = Http::post(config("services.smmworld.link"), ['key' =>
			config("services.smmworld.key"), 'action' => 'services']);
		$services = $services['services'];

		foreach ($services as $service) {
			if ($service['id'] == $id){
				$s = new Supplier;
				$s->service_id = $service_id;
				$s->supplier = 'smmworld';
				$s->supplier_id = $service['id'];
				$s->supplier_name = $service['name'];
				$s->min = $service['min'];
				$s->max = $service['max'];
				$s->cost = $service['price_per_k'] * 100;
				$s->save();
				return $s;
			} 
		}
	}

	//Fulfills an order with only the primary supplier of it's service
	public static function fulfill($order) {
		if (gettype($order) == 'integer') {
			$order = Order::find($order);
		}
	
		return Supply::orderPrimary($order);
	}

	//Fulfills an order with the first available supplier it finds. It returns
	//false if none could be found and the refreshed order model otherwise.
	public static function fulfillWithAny($order) {
		if (gettype($order) == 'integer') {
			$order = Order::find($order);
		}
		
		if (Supply::orderPrimary($order)) {
			return $order->refresh();
		};

		$suppliers = $order->service->suppliers;

		foreach ($suppliers as $s) {
			switch ($s->supplier) {
			case 'smmkings':
				if (Supply::orderSmmkings($order, $s)){
					return $order->refresh();
				}
				break;
			case 'smmworld':
				if (Supply::orderSmmkings($order, $s)){
					return $order->refresh();
				}
				break;
			}
			
		}

		return false;
	}

	//Fulfills an order with only the primary supplier of it's service
	public static function orderPrimary($order) {
		if (gettype($order) == 'integer') {
			$order = Order::find($order);
		}

		$primary = $order->service->primary;

		switch ($primary->supplier) {
		case 'smmkings':
			return Supply::orderSmmkings($order, $primary);
			break;
		case 'smmworld':
			return Supply::orderSmmworld($order, $primary);
			break;
		}
	}


	//Should check that cost is not greater than price and store order
	//information if completed. return false/NULL if an error occured
	public static function orderSmmkings($order, $supplier) {
		if ($supplier->supplier != 'smmkings') {
			return false;
		}

		//Check that the order is not already pending. Probably by calling
		//another function.
		
		//Check supplier cost here to make sure the request is a good idea

		$response = Http::post(config("services.smmkings.link"), ['key' =>
			config("services.smmkings.key"), 'action' => 'add', 
			'service' => $supplier->supplier_id, 'link' =>
			$order->url, 'quantity' => $order->quantity])->json();

		//This should log the error for later reporting
		if (array_key_exists('error', $response)){
			return;
		}

		//store order information here

		$status = Http::post(config("services.smmkings.link"), ['key' =>
			config("services.smmkings.key"), 'action' => 'status', 
			'order' => $response['order']])->json();

		return $response;
	}

	public static function orderSmmworld($order, $supplier) {
		if ($supplier->supplier != 'smmworld') {
			return false;
		}

		$response = Http::post(config("services.smmworld.link"), ['key' =>
			config("services.smmworld.key"), 'action' => 'add', 
			'service' => $supplier->supplier_id, 'link' => $order->url, 'quantity' =>
			$order->quantity])->json();

		return $response;
	}


}
