<?php

namespace App\Jobs;

use App\FaceSwapJob;
use App\Services\FaceSwapService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessFaceSwapInsightface implements ShouldQueue
{
	use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
	
	protected $faceSwapJob;
	
	/**
	 * The number of seconds the job can run before timing out.
	 *
	 * @var int
	 */
	public $timeout = 3600;
	
	/**
	 * Indicate if the job should be marked as failed on timeout.
	 *
	 * @var bool
	 */
	public $failOnTimeout = false;
	
	/**
	 * Create a new job instance.
	 *
	 * @param $sourcePath
	 * @param $targetPath
	 * @param $outputPath
	 */
	public function __construct(FaceSwapJob $faceSwapJob)
	{
		$this->onQueue(config('queue.connections.redis.queue'));
		$this->faceSwapJob = $faceSwapJob;
	}
	
	/**
	 * @return bool
	 */
	public function isFailOnTimeout(): bool
	{
		return $this->failOnTimeout;
	}
	
	/**
	 * Execute the job.
	 *
	 * @return void
	 */
	public function handle(FaceSwapService $service)
	{
		/*try {
			$job = FaceSwapJob::create([
				'code'        => $this->code,
				'source_path' => $this->sourcePath,
				'target_path' => $this->targetPath,
				'output_path' => $this->outputPath,
				'status'      => 'pending'
			]);
			
			$faceSwapPath = config('faceswap.faceswap_app_path');
			$pythonPath = $faceSwapPath . '/venv/bin/python';
			
			//$command = "source {$venvPath}/bin/activate && python {$faceSwapPath}/app.py --source {$this->sourcePath} --target {$this->targetPath} --output {$this->outputPath} --job-id {$job->id}";
			$command = "{$pythonPath} {$faceSwapPath}/app.py --source {$this->sourcePath} --target {$this->targetPath} --output {$this->outputPath} --job-id {$job->id} 2>/dev/null";
			exec($command);
		} catch (\Exception $exception) {
			logger("[Error] Insightface progress face swap image error: " . $exception->getMessage());
		}*/
		
		$service->swap($this->faceSwapJob);
	}
}
