<?php
namespace App\Libs;

use Config;
use Log;
use App\Models;
class PaperLessNgx {

	public function __construct(){
		//nothingness
	}

	public function sendToPaperless($file_path, $tags_arr, $created_at, $document_type, $title, $correspondent){
		$dir = getcwd();
		// dd($dir);
		$prod_url = "http://206.225.84.201:8000/api/documents/post_document/";
		$local_url = "http://192.168.68.152:8000/api/documents/post_document/";
		// dd($ip);
		if ($dir !== '/Users/kwesibanson/Sites/team_tracker') {
			$url = $prod_url;
			$auth = "cGxjbGlja2FkbWluOkJyYW5jaDIwMDA=";
		}
		else{
			$url = $local_url;
			$auth = "cGxhZG1pbjpCcmFuY2gyMDAw";
		}
		// dd($url);
		// $pop = new \CURLFILE('/Users/kwesibanson/Documents/36-PropertyBillsRegister-2019.pdf');
		// $curl_file = new \CURLFILE(public_path('documents/ultra_host.pdf'));
		$curl_file = new \CURLFILE(public_path($file_path));
		$curl = curl_init();
		$data = [
			"document" => $curl_file,
			'title' => $title,
			'tags' => $tags_arr,
			'correspondent' => $correspondent,
			'created' => $created_at,
			'document_type' => $document_type,
			'storage_path' => 1
		];

		if (count($tags_arr) > 0) {
			foreach ($tags_arr as $tag) {
				$data['tags'] = $tag;
			}
		}
		\Log::info($data);
		// dd($data);
		curl_setopt_array($curl, array(
			CURLOPT_URL => $url,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'POST',
			CURLOPT_POSTFIELDS => $data, //['document' => $curl_file],
			// CURLOPT_POSTFIELDS => array('document'=> new CURLFILE('/Users/kwesibanson/Documents/36-PropertyBillsRegister-2019.pdf')),
			CURLOPT_HTTPHEADER => array(': ', 'Authorization: Basic ' . $auth),
		));
		$response = curl_exec($curl);
		// print_r(curl_getinfo($ch));
		// dump(curl_errno($curl));
		// dump(curl_error($curl));
		// curl_close($curl);
		return $response;

	}
	public function getToPaperlessParamValues($name){
		// $name = "document_types/";
		$dir = getcwd();
		$prod_url = "http://206.225.84.201:8000/api/$name/";
		$local_url = "http://192.168.68.152:8000/api/$name/";
		// dd($ip);
		if ($dir !== '/Users/kwesibanson/Sites/team_tracker') {
			$url = $prod_url;
			$auth = "cGxjbGlja2FkbWluOkJyYW5jaDIwMDA=";
		}
		else{
			$url = $local_url;
			$auth = "cGxhZG1pbjpCcmFuY2gyMDAw";
		}
		// $pop = new \CURLFILE('/Users/kwesibanson/Documents/36-PropertyBillsRegister-2019.pdf');
		// $curl_file = new \CURLFILE(public_path('documents/ultra_host.pdf'));

		// curl --location 'http://206.225.84.201:8000/api/tags/' \
		// --header 'Authorization: Basic cGxjbGlja2FkbWluOkJyYW5jaDIwMDA=' \
		// --header 'Cookie: csrftoken=bDnOrmAk0cKFXwKEQ7LMl0fiTn8EZDqU'

		// dd($data);
		$curl = curl_init();
		curl_setopt_array($curl, array(
			CURLOPT_URL => $url,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_ENCODING => '',
			CURLOPT_MAXREDIRS => 10,
			CURLOPT_TIMEOUT => 0,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
			CURLOPT_CUSTOMREQUEST => 'GET',
			CURLOPT_HTTPHEADER => array(': ', 'Authorization: Basic ' . $auth),
		));
		$response = curl_exec($curl);
		curl_close($curl);
		return $response;

	}


	public function processPaperlessFile($title, $category, $filename, $store_location){
		# set parameters and push to paperless
		$tags_correspondent = $this->getDocumentCorrespondent($category);
		if ($store_location == "general_files") {
			$file_path = "documents/general_files/" . $filename;
		}
		else if ($store_location == "mno_files") {
			$file_path = "documents/mno_files/" . $filename;
		}
		else{
			$file_path = "documents/client_files/" . $filename;
		}

		$tags_arr[] = $tags_correspondent['tags']; //[1,2];
		$created_at = date('Y-m-d H:i:s');
		$document_type = $tags_correspondent['document_type'];
		$correspondent = $tags_correspondent['correspondent'];
		$retval_paperless = $this->sendToPaperless($file_path, $tags_arr, $created_at, $document_type, $title, $correspondent);
		\Log::info($retval_paperless);
		return true;
	}
	public function getDocumentCorrespondent($category){
		$category = strtolower($category);
		switch ($category) {
			case 'sidwl':
				$correspondent = "2";
				$tags = "9";
				$document_type = "3";
				break;
			case 'api':
				$correspondent = "1";
				$tags = "7";
				$document_type = "3";
				break;
			case 'contracts':
				$correspondent = "3";
				$tags = "8";
				$document_type = "4";
				break;
			case 'scfees':
				$correspondent = "2";
				$tags = "9";
				$document_type = "7";
				break;
			case 'presentations':
				$correspondent = "3";
				$tags = "1";
				$document_type = "1";
				break;
			case 'proposal':
				$correspondent = "3";
				$tags = "2";
				$document_type = "10";
				break;
			case 'rates':
				// code...General
				$correspondent = "6";
				$tags = "3";
				$document_type = "7";
				break;
			case 'non disclosure':
				$correspondent = "3";
				$tags = "8";
				$document_type = "4";
				break;
			case 'technical document':
				$correspondent = "1";
				$tags = "10";
				$document_type = "3";
				break;
			default:
				$correspondent = "3";
				$tags = "10";
				$document_type = "11";
				break;
		}
		$data = ['correspondent' => $correspondent, 'tags' => $tags, 'document_type' => $document_type];
		return $data;
	}




}
