<?php

namespace App\Http\Controllers\Ajax;

use App\Artist;
use App\AuctionObject;
use App\AuctionObjectImported;
use App\Helpers\AuctionObjectHelper;
use App\Helpers\AuctionObjectImportedHelper;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AuctionObjectImportedAssignmentController extends Controller
{

    /**
     * @param Request $request
     * @return string
     */
    public function assignMediumCategory(Request $request) {

        $auctionId = $request->auction_object_id;

        $auctionObjectImported = new AuctionObjectImported();
        $auctionObjectImported = $auctionObjectImported->find($auctionId);

        $auctionObjectImported->t_medium_category_id = $request->medium_category_id;
        $auctionObjectImported->save();

        //AuctionObjectImportedHelper::adjustStatus($auctionId);

        return '200';
    }

    /**
     * @param Request $request
     * @return string
     */
    public function assignArtist(Request $request) {

        // Auktionsobjekt speichern
        $auctionId = $request->auction_object_id;
        $artistId = $request->artist_id;

        $auctionObjectImported = new AuctionObjectImported();
        $auctionObjectImported = $auctionObjectImported->find($auctionId);

        $auctionObjectImported->t_artist_id = $artistId;
        $auctionObjectImported->save();

        AuctionObjectImportedHelper::adjustStatus($auctionId);

        //AuctionObjectImportedHelper::saveManuallyArtistAssignment($auctionId);

        // Artist Daten ermitteln und zurueckgeben
        $artist = new Artist();
        $artist = $artist->find($artistId);

        $nameDE = $artist->firstname_de . ' ' . $artist->lastname_de;
        $nameEN = $artist->firstname_en . ' ' . $artist->lastname_en;
        $nameFR = $artist->firstname_fr . ' ' . $artist->lastname_fr;

        if (strlen($nameDE) > 1) {

            return $nameDE;
        } elseif (strlen($nameDE) <= 1 && strlen($nameEN) > 1) {

            return $nameEN;
        } elseif (strlen($nameDE) <= 1 && strlen($nameEN) <= 1 && strlen($nameFR) > 1) {

            return $nameFR;
        } else {

            return 'V.A.';
        }
    }

    /**
     * @param Request $request
     * @return string
     */
    public function deleteArtistAssignment(Request $request) {

        $auctionId = $request->auction_object_id;

        $auctionObjectImported = new AuctionObjectImported();
        $auctionObjectImported = $auctionObjectImported->find($auctionId);

        $auctionObjectImported->t_artist_id = null;
        $auctionObjectImported->save();

        AuctionObjectImportedHelper::adjustStatus($auctionId);

        //AuctionObjectHelper::deleteManuallyArtistAssignment($auctionId);

        return '200';
    }

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

        $auctionId = $request->auction_object_id;

        $auctionObject = new AuctionObject();
        $auctionObject = $auctionObject->find($auctionId);

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

        return '200';
    }


}
