<?php

namespace App\Http\Controllers\Admin;

use App\Album;
use App\AlbumDefaultAnonymousPermission;
use App\AlbumDefaultGroupPermission;
use App\AlbumDefaultUserPermission;
use App\AlbumRedirect;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Group;
use App\Helpers\DbHelper;
use App\Helpers\FileHelper;
use App\Helpers\MiscHelper;
use App\Helpers\PermissionsHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Label;
use App\Permission;
use App\Photo;
use App\Services\AlbumService;
use App\Services\PhotoService;
use App\Storage;
use App\User;
use App\UserActivity;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;

class AlbumController extends Controller
{
    public static function doesGroupHaveDefaultPermission(Group $group, Permission $permission)
    {
        return AlbumDefaultGroupPermission::where([
                'group_id' => $group->id,
                'permission_id' => $permission->id
            ])->count() > 0;
    }

    public static function doesUserHaveDefaultPermission($user, Permission $permission)
    {
        // User will be null for anonymous users
        if (is_null($user))
        {
            return AlbumDefaultAnonymousPermission::where(['permission_id' => $permission->id])->count() > 0;
        }
        else
        {
            return AlbumDefaultUserPermission::where([
                    'user_id' => $user->id,
                    'permission_id' => $permission->id
                ])->count() > 0;
        }
    }

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

    public function analyse($id, $queue_token)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-albums');

        $album = $this->loadAlbum($id, 'upload-photos');
        $photos = $album->photos()
            ->where('is_analysed', false)
            ->orderBy('created_at')
            ->get();

        if (count($photos) == 0)
        {
            return redirect(route('albums.show', ['id' => $album->id]));
        }

        return Theme::render('admin.analyse_album', ['album' => $album, 'photos' => $photos, 'queue_token' => $queue_token]);
    }

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

        $albumSources = [];
        foreach (Storage::where('is_active', true)->orderBy('name')->get() as $storage)
        {
            $albumSources[$storage->id] = $storage->name;
        }

        if (count($albumSources) == 0)
        {
            $request->session()->flash('info', trans('admin.create_album_no_storage'));
            return redirect(route('storage.create'));
        }

        $albumService = new AlbumService();
        $defaultSource = Storage::where('is_default', true)->limit(1)->first();

        return Theme::render('admin.create_album', [
            'album_sources' => $albumSources,
            'default_storage_id' => (!is_null($defaultSource) ? $defaultSource->id : 0),
            'parent_albums' => $albumService->getFlattenedAlbumTree()
        ]);
    }

    public function defaultPermissions()
    {
        $this->authorizeAccessToAdminPanel('admin:manage-albums');

        $addNewGroups = [];
        $existingGroups = [];
        foreach (Group::orderBy('name')->get() as $group)
        {
            if (AlbumDefaultGroupPermission::where('group_id', $group->id)->count() == 0)
            {
                $addNewGroups[] = $group;
            }
            else
            {
                $existingGroups[] = $group;
            }
        }

        $existingUsers = [];
        foreach (User::orderBy('name')->get() as $user)
        {
            if (AlbumDefaultUserPermission::where('user_id', $user->id)->count() > 0)
            {
                $existingUsers[] = $user;
            }
        }

        return Theme::render('admin.album_default_permissions', [
            'add_new_groups' => $addNewGroups,
            'all_permissions' => Permission::where('section', 'album')->get(),
            'existing_groups' => $existingGroups,
            'existing_users' => $existingUsers
        ]);
    }

    public function delete($id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-albums');

        $album = $this->loadAlbum($id, 'delete');

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

    public function deleteRedirect(Request $request, $id, $redirectId)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-albums');

        $album = $this->loadAlbum($id, 'delete');

        /** @var AlbumRedirect $redirect */
        $redirect = $album->redirects()->where('id', $redirectId)->first();
        if (is_null($redirect))
        {
            App::abort(404);
            return;
        }

        $redirect->delete();
        $request->session()->flash('success', trans('admin.delete_redirect_success_message'));
        return redirect(route('albums.show', ['id' => $id, 'tab' => 'redirects']));
    }

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

        $album = $this->loadAlbum($id, 'delete');

        if ($album->children()->count() > 0)
        {
            $request->session()->flash('error', trans('admin.delete_album_failed_children', ['album' => $album->name]));
            return redirect(route('albums.index'));
        }

        // Delete all the photo files
        /** @var Photo $photo */
        foreach ($album->photos as $photo)
        {
            $photoService = new PhotoService($photo);
            $photoService->delete();
        }

        $album->getAlbumSource()->deleteAlbumContents();
        $album->delete();

        $request->session()->flash('success', trans('admin.delete_album_success_message', ['album' => $album->name]));

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

    /**
     * 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-albums');

        $album = $this->loadAlbum($id);

        if (!$request->session()->has('_old_input'))
        {
            $request->session()->flash('_old_input', $album->toArray());
        }

        $albumService = new AlbumService();

        return Theme::render('admin.edit_album', [
            'album' => $album,
            'parent_albums' => $albumService->getFlattenedAlbumTree()
        ]);
    }

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

        // Only get top-level albums
        $albums = DbHelper::getAlbumsForCurrentUser(0);
        foreach ($albums as $album)
        {
            $this->loadChildAlbums($album);
        }

        return Theme::render('admin.list_albums', [
            'albums' => $albums,
            'success' => $request->session()->get('success'),
        ]);
    }

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

        /** @var Album $album */
        $album = $this->loadAlbum($id);

        $photosNeededToUpdate = $album->photos()->where('metadata_version', '<', PhotoService::METADATA_VERSION)->get();

        return Theme::render('admin.album_metadata', [
            'album' => $album,
            'current_metadata' => PhotoService::METADATA_VERSION,
            'photos' => $photosNeededToUpdate,
            'queue_token' => MiscHelper::randomString()
        ]);
    }

    public function setDefaultGroupPermissions(Request $request)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-albums');

        if ($request->get('action') == 'add_group' && $request->has('group_id'))
        {
            /* Add a new group to the default permission list */

            /** @var Group $group */
            $group = Group::where('id', $request->get('group_id'))->first();
            if (is_null($group))
            {
                App::abort(404);
            }

            // Link all default permissions to the group
            /** @var Permission $permission */
            foreach (Permission::where(['section' => 'album', 'is_default' => true])->get() as $permission)
            {
                $defaultPermission = new AlbumDefaultGroupPermission();
                $defaultPermission->group_id = $group->id;
                $defaultPermission->permission_id = $permission->id;
                $defaultPermission->save();
            }
        }
        else if ($request->get('action') == 'update_group_permissions')
        {
            /* Update existing group permissions for this album */
            AlbumDefaultGroupPermission::truncate();

            $permissions = $request->get('permissions');
            if (is_array($permissions))
            {
                foreach ($permissions as $groupID => $permissionIDs)
                {
                    foreach ($permissionIDs as $permissionID)
                    {
                        $defaultPermission = new AlbumDefaultGroupPermission();
                        $defaultPermission->group_id = $groupID;
                        $defaultPermission->permission_id = $permissionID;
                        $defaultPermission->save();
                    }
                }
            }
        }

        // Rebuild the permissions cache
        $helper = new PermissionsHelper();
        $helper->rebuildCache();

        return redirect(route('albums.defaultPermissions'));
    }

    public function setDefaultUserPermissions(Request $request)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-albums');

        if ($request->get('action') == 'add_user' && $request->has('user_id'))
        {
            /* Add a new user to the permission list for this album */

            /** @var User $user */
            $user = User::where('id', $request->get('user_id'))->first();
            if (is_null($user))
            {
                App::abort(404);
            }

            // Link all default permissions to the group
            /** @var Permission $permission */
            foreach (Permission::where(['section' => 'album', 'is_default' => true])->get() as $permission)
            {
                $defaultPermission = new AlbumDefaultUserPermission();
                $defaultPermission->user_id = $user->id;
                $defaultPermission->permission_id = $permission->id;
                $defaultPermission->save();
            }
        }
        else if ($request->get('action') == 'update_user_permissions')
        {
            /* Update existing user and anonymous permissions for this album */
            AlbumDefaultAnonymousPermission::truncate();
            AlbumDefaultUserPermission::truncate();

            $permissions = $request->get('permissions');
            if (is_array($permissions))
            {
                if (isset($permissions['anonymous']))
                {
                    foreach ($permissions['anonymous'] as $permissionID)
                    {
                        $defaultPermission = new AlbumDefaultAnonymousPermission();
                        $defaultPermission->permission_id = $permissionID;
                        $defaultPermission->save();
                    }
                }

                foreach ($permissions as $key => $value)
                {
                    $userID = intval($key);
                    if ($userID == 0)
                    {
                        // Skip non-numeric IDs (e.g. anonymous)
                        continue;
                    }

                    foreach ($value as $permissionID)
                    {
                        $defaultPermission = new AlbumDefaultUserPermission();
                        $defaultPermission->user_id = $userID;
                        $defaultPermission->permission_id = $permissionID;
                        $defaultPermission->save();
                    }
                }
            }
        }

        // Rebuild the permissions cache
        $helper = new PermissionsHelper();
        $helper->rebuildCache();

        return redirect(route('albums.defaultPermissions'));
    }

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

        /** @var Album $album */
        $album = $this->loadAlbum($id, 'change-permissions');

        if ($request->get('action') == 'add_group' && $request->has('group_id'))
        {
            /* Add a new group to the permission list for this album */

            /** @var Group $group */
            $group = Group::where('id', $request->get('group_id'))->first();
            if (is_null($group))
            {
                App::abort(404);
            }

            // Link all default permissions to the group
            /** @var Permission $permission */
            foreach (Permission::where(['section' => 'album', 'is_default' => true])->get() as $permission)
            {
                $album->groupPermissions()->attach($permission->id, [
                    'group_id' => $group->id,
                    'created_at' => new \DateTime(),
                    'updated_at' => new \DateTime()
                ]);
            }
        }
        else if ($request->get('action') == 'update_group_permissions')
        {
            /* Update existing group permissions for this album */
            $album->groupPermissions()->detach();

            $permissions = $request->get('permissions');
            if (is_array($permissions))
            {
                foreach ($permissions as $groupID => $permissionIDs)
                {
                    foreach ($permissionIDs as $permissionID)
                    {
                        $album->groupPermissions()->attach($permissionID, [
                            'group_id' => $groupID,
                            'created_at' => new \DateTime(),
                            'updated_at' => new \DateTime()
                        ]);
                    }
                }
            }
        }

        $album->save();

        // Rebuild the permissions cache
        $helper = new PermissionsHelper();
        $helper->rebuildCache();

        return redirect(route('albums.show', [$album->id, 'tab' => 'permissions']));
    }

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

        /** @var Album $album */
        $album = $this->loadAlbum($id, 'change-permissions');

        if ($request->get('action') == 'add_user' && $request->has('user_id'))
        {
            /* Add a new user to the permission list for this album */

            /** @var User $user */
            $user = User::where('id', $request->get('user_id'))->first();
            if (is_null($user))
            {
                App::abort(404);
            }

            // Link all default permissions to the group
            /** @var Permission $permission */
            foreach (Permission::where(['section' => 'album', 'is_default' => true])->get() as $permission)
            {
                $album->userPermissions()->attach($permission->id, [
                    'user_id' => $user->id,
                    'created_at' => new \DateTime(),
                    'updated_at' => new \DateTime()
                ]);
            }
        }
        else if ($request->get('action') == 'update_user_permissions')
        {
            /* Update existing user and anonymous permissions for this album */
            $album->anonymousPermissions()->detach();
            $album->userPermissions()->detach();

            $permissions = $request->get('permissions');
            if (is_array($permissions))
            {
                if (isset($permissions['anonymous']))
                {
                    foreach ($permissions['anonymous'] as $permissionID)
                    {
                        $album->anonymousPermissions()->attach($permissionID, [
                            'created_at' => new \DateTime(),
                            'updated_at' => new \DateTime()
                        ]);
                    }
                }

                foreach ($permissions as $key => $value)
                {
                    $userID = intval($key);
                    if ($userID == 0)
                    {
                        // Skip non-numeric IDs (e.g. anonymous)
                        continue;
                    }

                    foreach ($value as $permissionID)
                    {
                        $album->userPermissions()->attach($permissionID, [
                            'user_id' => $userID,
                            'created_at' => new \DateTime(),
                            'updated_at' => new \DateTime()
                        ]);
                    }
                }
            }
        }

        $album->save();

        // Rebuild the permissions cache
        $helper = new PermissionsHelper();
        $helper->rebuildCache();

        return redirect(route('albums.show', [$album->id, 'tab' => 'permissions']));
    }

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

        $album = $this->loadAlbum($id);
        $photos = $album->photos()
            ->orderBy(DB::raw('COALESCE(taken_at, created_at)'))
            ->paginate(UserConfig::get('items_per_page_admin'));

        // See if we can upload (need the GD extension)
        $isUploadEnabled = extension_loaded('gd');
        $fileUploadLimit = MiscHelper::convertToBytes(ini_get('upload_max_filesize')) / (1024*1024);
        $postLimit = MiscHelper::convertToBytes(ini_get('post_max_size')) / (1024*1024);
        $fileUploadOrPostLowerLimit = ($postLimit < $fileUploadLimit) ? $postLimit : $fileUploadLimit;

        $allowedAlbumViews = [];
        foreach (UserConfig::allowedAlbumViews() as $view)
        {
            $allowedAlbumViews[$view] = trans(sprintf('gallery.album_views.%s', $view));
        }

        $addNewGroups = [];
        $existingGroups = [];
        foreach (Group::orderBy('name')->get() as $group)
        {
            if ($album->groupPermissions()->where('group_id', $group->id)->count() == 0)
            {
                $addNewGroups[] = $group;
            }
            else
            {
                $existingGroups[] = $group;
            }
        }

        $existingUsers = [];
        foreach (User::orderBy('name')->get() as $user)
        {
            if ($album->userPermissions()->where('user_id', $user->id)->count() > 0)
            {
                $existingUsers[] = $user;
            }
        }

        $activeTab = $request->get('tab');

        if (!$request->session()->has('_old_input'))
        {
            $request->session()->flash('_old_input', $album->toArray());
        }

        // Get the cameras used in this album
        $cameras = $album->cameras();

        return Theme::render('admin.show_album', [
            'active_tab' => (strlen($activeTab) == 0) ? 'photos' : $activeTab,
            'album' => $album,
            'add_new_groups' => $addNewGroups,
            'all_permissions' => Permission::where('section', 'album')->get(),
            'allowed_views' => $allowedAlbumViews,
            'bulk_actions' => [
                'rotate_left' => trans('admin.photo_actions.rotate_left'),
                'rotate_right' => trans('admin.photo_actions.rotate_right'),
                '-' => '-----',
                'flip_horizontal' => trans('admin.photo_actions.flip_horizontal'),
                'flip_vertical' => trans('admin.photo_actions.flip_vertical'),
                'flip_both' => trans('admin.photo_actions.flip_both'),
                '--' => '-----',
                'change_album' => trans('admin.photo_actions.change_album'),
                'refresh_thumbnails' => trans('admin.photo_actions.refresh_thumbnails'),
                'delete' => trans('admin.photo_actions.delete')
            ],
            'cameras' => $cameras,
            'error' => $request->session()->get('error'),
            'existing_groups' => $existingGroups,
            'existing_users' => $existingUsers,
            'file_upload_limit' => $fileUploadLimit,
            'is_upload_enabled' => $isUploadEnabled,
            'labels' => Label::all(),
            'max_post_limit' => $postLimit,
            'max_post_limit_bulk' => $fileUploadOrPostLowerLimit,
            'photos' => $photos,
            'queue_token' => MiscHelper::randomString(),
            'success' => $request->session()->get('success'),
            'warning' => $request->session()->get('warning')
        ]);
    }

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

        $album = new Album();
        $album->fill($request->only(['name', 'description', 'storage_id', 'parent_album_id']));
        $album->is_permissions_inherited = (strtolower($request->get('is_permissions_inherited')) == 'on');

        if (strlen($album->parent_album_id) == 0)
        {
            $album->parent_album_id = null;
        }

        $album->default_view = UserConfig::get('default_album_view');
        $album->user_id = Auth::user()->id;

        $album->generateAlias();
        $album->generateUrlPath();
        $album->save();

        // Link the default permissions (if a public album)
        $isPrivate = (strtolower($request->get('is_private')) == 'on');
        if (!$album->is_permissions_inherited && !$isPrivate)
        {
            $defaultAlbumUserPermissions = AlbumDefaultUserPermission::all();
            $defaultAlbumGroupPermissions = AlbumDefaultGroupPermission::all();
            $defaultAnonPermissions = AlbumDefaultAnonymousPermission::all();

            /** @var AlbumDefaultAnonymousPermission $permission */
            foreach ($defaultAnonPermissions as $permission)
            {
                $album->anonymousPermissions()->attach($permission->permission_id, [
                    'created_at' => new \DateTime(),
                    'updated_at' => new \DateTime()
                ]);
            }

            /** @var AlbumDefaultGroupPermission $permission */
            foreach ($defaultAlbumGroupPermissions as $permission)
            {
                $album->groupPermissions()->attach($permission->permission_id, [
                    'group_id' => $permission->group_id,
                    'created_at' => new \DateTime(),
                    'updated_at' => new \DateTime()
                ]);
            }

            /** @var AlbumDefaultUserPermission $permission */
            foreach ($defaultAlbumUserPermissions as $permission)
            {
                $album->userPermissions()->attach($permission->permission_id, [
                    'user_id' => $permission->user_id,
                    'created_at' => new \DateTime(),
                    'updated_at' => new \DateTime()
                ]);
            }
        }

        // Add an activity record
        $this->createActivityRecord($album, 'album.created');

        // Rebuild the permissions cache
        $helper = new PermissionsHelper();
        $helper->rebuildCache();

        return redirect(route('albums.show', ['id' => $album->id]));
    }

    public function storeRedirect(Requests\StoreAlbumRedirectRequest $request, $id)
    {
        $this->authorizeAccessToAdminPanel('admin:manage-albums');

        $album = $this->loadAlbum($id);

        $redirect = new AlbumRedirect();
        $redirect->fill($request->only('source_url'));
        $redirect->album_id = $album->id;
        $redirect->save();

        $request->session()->flash('success', trans('admin.create_redirect_success_message'));
        return redirect(route('albums.show', ['id' => $id, 'tab' => 'redirects']));
    }

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

        $album = $this->loadAlbum($id);
        $currentParentID = $album->parent_album_id;

        $album->fill($request->only(['name', 'description', 'parent_album_id']));
        $album->is_permissions_inherited = (strtolower($request->get('is_permissions_inherited')) == 'on');

        if (strlen($album->parent_album_id) == 0)
        {
            $album->parent_album_id = null;
        }

        // These keys are optional and may or may not be in the request, depending on the page requesting it
        foreach (['storage_id', 'default_view'] as $key)
        {
            if ($request->has($key))
            {
                $album->$key = $request->get($key);
            }
        }

        // Re-generate the URL path to ensure it's correct if the parent has changed
        if ($currentParentID != $album->parent_album_id)
        {
            // Create a redirect if required
            $redirectData = [
                'album_id' => $album->id,
                'source_url' => sprintf('/%s', $album->url_path)
            ];

            if (strtolower($request->get('preserve_url_redirect')) == 'on' && AlbumRedirect::where($redirectData)->count() == 0)
            {
                $redirect = new AlbumRedirect();
                $redirect->fill($redirectData);
                $redirect->save();
            }

            // Update the URL path
            $album->generateUrlPath();
        }
        
        $album->save();

        // Rebuild the permissions cache
        $helper = new PermissionsHelper();
        $helper->rebuildCache();

        $request->session()->flash('success', trans('admin.album_saved_successfully', ['name' => $album->name]));

        return redirect(route('albums.show', ['id' => $id]));
    }

    private function createActivityRecord(Album $album, $type, $activityDateTime = null)
    {
        if (is_null($activityDateTime))
        {
            $activityDateTime = new \DateTime();
        }

        $userActivity = new UserActivity();
        $userActivity->user_id = $this->getUser()->id;
        $userActivity->activity_at = $activityDateTime;
        $userActivity->type = $type;
        $userActivity->album_id = $album->id;
        $userActivity->save();
    }

    /**
     * @param $id
     * @return Album
     */
    private function loadAlbum($id, $permission = 'edit')
    {
        $album = Album::where('id', intval($id))->first();
        if (is_null($album))
        {
            App::abort(404);
            return null;
        }

        $this->authorize($permission, $album);

        return $album;
    }

    private function loadChildAlbums(Album $album)
    {
        $album->child_albums = DbHelper::getChildAlbums($album);
        foreach ($album->child_albums as $childAlbum)
        {
            $this->loadChildAlbums($childAlbum);
        }
    }
}