<?php

namespace App\Http\Controllers\Admin;

use App\ExternalService;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Services\DropboxService;
use App\Storage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\View;

class StorageController extends Controller
{
    /**
     * @var mixed
     */
    private $encryptedFields;

    public function __construct()
    {
        $this->middleware('auth');
        View::share('is_admin', true);

        $this->encryptedFields = ['password', 'access_key', 'secret_key', 'access_token'];
    }

    public function authoriseService(Request $request, $id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        $storage = Storage::where('id', intval($id))->first();
        if (is_null($storage))
        {
            App::abort(404);
        }

        $externalServiceType = $this->getExternalServiceType($storage);

        if (is_null($externalServiceType))
        {
            $request->session()->flash('error', trans('admin.storage_no_external_service_support'));
            return redirect(route('storages.index'));
        }

        $serviceTypeName = trans(sprintf('services.%s', $externalServiceType));
        $viewData = [
            'service' => $storage->externalService,
            'serviceName' => $serviceTypeName,
            'storage' => $storage
        ];

        switch ($externalServiceType)
        {
            case ExternalService::DROPBOX:
                $dropbox = new DropboxService();
                $viewData['authoriseUrl'] = $dropbox->authoriseUrl($storage);
                $viewData['callbackUrl'] = $dropbox->callbackUrl();
                break;

            default:
                $request->session()->flash('error', trans('admin.storage_external_service_no_authorisation', ['service_name' => $serviceTypeName]));
                return redirect(route('storages.index'));
        }

        return Theme::render('admin.authorise_external_service', $viewData);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        $storageLocations = Storage::orderBy('name')
            ->paginate(UserConfig::get('items_per_page'));

        return Theme::render('admin.list_storage', [
            'error' => $request->session()->get('error'),
            'storageLocations' => $storageLocations,
            'warning' => $request->session()->get('warning'),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        $filesystemDefaultLocation = sprintf('%s/storage/app/albums', dirname(dirname(dirname(dirname(__DIR__)))));
        $storage = new Storage();
        $storage->s3_signed_urls = true;

        return Theme::render('admin.create_storage', [
            'album_sources' => UserConfig::albumSources(),
            'dropbox_services' => ExternalService::getForService(ExternalService::DROPBOX),
            'filesystem_default_location' => $filesystemDefaultLocation,
            'info' => $request->session()->get('info'),
            'storage' => $storage
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Requests\StoreStorageRequest $request)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        $storage = new Storage();
        $storage->fill($request->only([
            'name',
            'source',
            'location',
            'auth_url',
            'tenant_name',
            'username',
            'password',
            'service_name',
            'service_region',
            'container_name',
            'cdn_url',
            'access_key',
            'secret_key',
            'b2_bucket_type',
            'external_service_id'
        ]));
        $storage->is_active = true;
        $storage->is_default = (strtolower($request->get('is_default')) == 'on');
        $storage->is_internal = false;
        $storage->s3_signed_urls = (strtolower($request->get('s3_signed_urls')) == 'on');

        if ($storage->source != 'LocalFilesystemSource' && isset($storage->location))
        {
            unset($storage->location);
        }

        foreach ($this->encryptedFields as $field)
        {
            if (isset($storage->$field) && !empty($storage->$field))
            {
                $storage->$field = encrypt($storage->$field);
            }
        }

        $storage->save();

        if ($storage->is_default)
        {
            $this->unsetIsDefaultFromOthers($storage);
        }

        $externalServiceType = $this->getExternalServiceType($storage);

        if (!is_null($externalServiceType))
        {
            switch ($externalServiceType)
            {
                case ExternalService::DROPBOX:
                    return redirect(route('storage.authoriseService', ['storage' => $storage->id]));
            }
        }

        return redirect(route('storage.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //public function show($id)
    //{
        //
    //}

    /**
     * Show the form for deleting the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request, $id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        $storage = Storage::where('id', intval($id))->first();
        if (is_null($storage))
        {
            App::abort(404);
        }

        if ($storage->is_internal)
        {
            // Can't delete the default storage location
            $request->session()->flash('warning', trans('admin.delete_storage_internal'));
            return redirect(route('storage.index'));
        }

        if ($storage->albums()->count() > 0)
        {
            // Can't delete storage location while albums exist
            $request->session()->flash('error', trans('admin.delete_storage_existing_albums'));
            return redirect(route('storage.index'));
        }

        return Theme::render('admin.delete_storage', ['storage' => $storage]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        /** @var Storage $storage */
        $storage = Storage::where('id', intval($id))->first();
        if (is_null($storage))
        {
            App::abort(404);
        }

        foreach ($this->encryptedFields as $field)
        {
            if (isset($storage->$field) && !empty($storage->$field))
            {
                $storage->$field = decrypt($storage->$field);
            }
        }

        return Theme::render('admin.edit_storage', [
            'dropbox_services' => ExternalService::getForService(ExternalService::DROPBOX),
            'storage' => $storage
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Requests\StoreStorageRequest $request, $id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        $storage = Storage::where('id', intval($id))->first();
        if (is_null($storage))
        {
            App::abort(404);
        }

        $storage->fill($request->only([
            'name',
            'auth_url',
            'tenant_name',
            'username',
            'password',
            'service_name',
            'service_region',
            'container_name',
            'cdn_url',
            'access_key',
            'secret_key',
            'b2_bucket_type',
            'external_service_id'
        ]));
        $storage->is_active = (strtolower($request->get('is_active')) == 'on');
        $storage->is_default = (strtolower($request->get('is_default')) == 'on');
        $storage->s3_signed_urls = (strtolower($request->get('s3_signed_urls')) == 'on');

        if ($storage->is_default && !$storage->is_active)
        {
            $storage->is_default = false;
        }

        foreach ($this->encryptedFields as $field)
        {
            if (isset($storage->$field) && !empty($storage->$field))
            {
                $storage->$field = encrypt($storage->$field);
            }
        }

        $storage->save();

        if ($storage->is_default)
        {
            $this->unsetIsDefaultFromOthers($storage);
        }
        else
        {
            $this->setIsDefaultForFirstStorage();
        }

        return redirect(route('storage.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-storage');

        $storage = Storage::where('id', intval($id))->first();
        if (is_null($storage))
        {
            App::abort(404);
        }

        if ($storage->is_internal)
        {
            // Can't delete the default storage location
            $request->session()->flash('warning', trans('admin.delete_storage_internal'));
            return redirect(route('storage.index'));
        }

        if ($storage->albums()->count() > 0)
        {
            // Can't delete storage location while albums exist
            $request->session()->flash('error', trans('admin.delete_storage_existing_albums'));
            return redirect(route('storage.index'));
        }

        $storage->delete();

        return redirect(route('storage.index'));
    }

    private function getExternalServiceType(Storage $storage)
    {
        if (!is_null($storage->externalService))
        {
            return $storage->externalService->service_type;
        }

        return null;
    }

    private function setIsDefaultForFirstStorage()
    {
        $count = Storage::where('is_default', true)->count();

        if ($count == 0)
        {
            $storage = Storage::where('is_active', true)->first();

            if (!is_null($storage))
            {
                $storage->is_default = true;
                $storage->save();
            }
        }
    }

    private function unsetIsDefaultFromOthers(Storage $storage)
    {
        // If this storage is flagged as default, remove all others
        foreach (Storage::all() as $otherStorage)
        {
            if ($otherStorage->id == $storage->id)
            {
                // Ignore the one just created
                continue;
            }

            $otherStorage->is_default = false;
            $otherStorage->save();
        }
    }
}