<?php

namespace App\PackagesPublic\Capsule\Controllers;

use App\Http\Controllers\Controller;
use App\Packages\Capsule\Transformer\CapsulePublicTransformer;
use App\Packages\v4\Capsule\Repository\CapsuleRepository;
use App\PackagesPublic\Capsule\Services\ShareStatisticService;
use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class CapsuleController extends Controller {

    public function share(Request $request, $uuid) {
        $capsule = CapsuleRepository::init()->firstByUuid($uuid);
        $key = $request->get('share_key');

        if (!$capsule) throw new NotFoundHttpException();
        if (!isset($key) || $key != $capsule->share_key) throw new NotFoundHttpException();

        $shareStatistic = new ShareStatisticService($capsule);
        $shareStatistic->addWebView();

        $data = CapsulePublicTransformer::mainCapsule($capsule);

        $agent = new Agent();
        if ($agent->is('AndroidOS')) $data['platform'] = 'android';
        if ($agent->is('OS X')) $data['platform'] = 'ios';

        return view('public.capsule.share', $data);
    }

}