<?php

namespace App\Http\Controllers\Ajax;

use App\Artist;
use App\AuctionObject;
use App\Helpers\AuctionObjectHelper;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ArtistAssignmentController extends Controller
{

    /**
     * Assign artist status
     *
     * @param Request $request
     * @return string
     */
    public function assignStatus(Request $request) {

        $artistId = $request->artist_id;

        $artist = new Artist();
        $artist = $artist->find($artistId);

        $artist->status = $request->status;
        $artist->save();

        if ($request->status == 1) {
            $this->restCallCheckArtistFirstnamesInTGenderLookupAndSaveArtistFirstname($request, $artistId);
        }

        if ($request->status == 1 || $request->status == 2) {
            //$this->restCallMatchAllArtists($request);
            $this->restCallMatchAndPrepareOneTartistForAllTauctionObjects($request, $artistId);
        }

        return '200';
    }

    /**
     * Assign artist gender
     *
     * @param Request $request
     * @return string
     */
    public function assignGender(Request $request) {

        $artistId = $request->artist_id;

        $artist = new Artist();
        $artist = $artist->find($artistId);

        $artist->gender_id = $request->gender_id;
        $artist->save();

        return '200';
    }

    /**
     *
     */
    private function restCallMatchAndPrepareOneTartistForAllTauctionObjects(Request $request, $artistId) {

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => env('SERVICE_URL_MATCHANDPREPAREONETARTISTFORALLTAUCTIONOBJECTS').'?tArtistId='.$artistId,
            CURLOPT_USERPWD => env('SERVICE_USERPASSWORD'),
            CURLOPT_ENCODING => "",
            //CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 10000,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "",//$jsonData,
            CURLOPT_HTTPHEADER => array(
                "accept: */*",
                "accept-language: en-US,en;q=0.8",
                "content-type: application/json",
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        $info = curl_getinfo($curl);
        $httpStatusCode = $info['http_code'];

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        }
    }

    /**
     * Call match all artists rest service
     */
    private function restCallMatchAllArtists(Request $request) {

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => env('SERVICE_URL_MATCHALLARTISTS'),
            CURLOPT_USERPWD => env('SERVICE_USERPASSWORD'),
            CURLOPT_ENCODING => "",
            CURLOPT_TIMEOUT => 5000,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "",
            CURLOPT_HTTPHEADER => array(
                "accept: */*",
                "accept-language: en-US,en;q=0.8",
                "content-type: application/json",
            ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        $info = curl_getinfo($curl);
        $httpStatusCode = $info['http_code'];

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        }
    }


    /**
     * Call rest service checkArtistFirstnamesInTGenderLookupAndSaveArtistFirstname
     *
     * @param Request $request Request
     * @param $artistId Artist ID
     */
    private function restCallCheckArtistFirstnamesInTGenderLookupAndSaveArtistFirstname(Request $request, $artistId) {

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => env('SERVICE_URL_CHECKARTISTFIRSTNAMESINTGENDERLOOKUPANDSAVEARTISTFIRSTNAME').'?tArtistId='.$artistId,
            CURLOPT_USERPWD => env('SERVICE_USERPASSWORD'),
            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_HTTPHEADER => array(
                "accept: */*",
                "accept-language: en-US,en;q=0.8",
                "content-type: application/json",
            ),
        ));

        $response = curl_exec($curl);

        $err = curl_error($curl);
        $info = curl_getinfo($curl);
        $httpStatusCode = $info['http_code'];

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        }
    }
}
