<?php

namespace App\Http\Controllers;

use App\Entity;
use App\EntityClass;

use App\Http\ViewHelpers\ViewFeeder;
use App\Http\Requests\EntityTypeRequest;
use App\Permission;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use DB;
use Illuminate\Validation\Rule;
use Input;
use Lang;
use Redirect;
use Session;

/**
 * Class EntityTypeController
 * @package App\Http\Controllers
 */
class EntityTypeController extends Controller implements ViewFeederAwareController
{
    /**
     * Create a new controller instance.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * @return \Illuminate\Http\RedirectResponse
     */
    public function indexDistinguish()
    {
        $firstEntityType = DB::table('entities as d')
            ->join('entity_classes as dc', 'd.entity_class_id', '=', 'dc.id')
            ->select('dc.id as id')
            ->where([
                ['dc.is_type', '=', 1]
            ])
            ->orderBy('dc.id', 'asc')
            ->first();

        if ($firstEntityType)
            return Redirect::route(self::getRouterName() . '.index', $firstEntityType->id);
        else {
            Session::flash('error', "No entity type was not found, you can create new one here.");
            return Redirect::route(self::getRouterName() . '.createDistinguish');
        }
    }

    /**
     * Show the screen for choosing the entity to be created.
     *
     * @return \Illuminate\Http\Response
     */
    public function createDistinguish()
    {
        $viewFeeder = new ViewFeeder($this, ViewFeeder::CREATE);

        $entityClasses = DB::table('entity_classes as dc')
            ->select([
                'dc.id as id',
                'dc.name as name',
            ])
            ->where([
                ['dc.is_type', '=', 1]
            ])
            ->distinct()
            ->orderBy('name', 'asc')
            ->get();

        return view('entity.createDistinguish', $viewFeeder
            ->size(4)
            ->results($entityClasses)
            ->feed());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param $classId
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
     */
    public function create($classId)
    {
        $viewFeeder = new ViewFeeder($this, ViewFeeder::CREATE);

        $entityClass = EntityClass::get($classId, 1, true, true);

        if (!$entityClass) {
            Session::flash('error', "Entity type with class id '$classId' not found.");
            return Redirect::route(self::getRouterName() . '.createDistinguish');
        }

        if ($entityClass instanceof RedirectResponse) {
            Input::flash();
            return $entityClass;
        }
        return view('entity.create', $viewFeeder
            ->size(6)
            ->result($entityClass)
            ->singular($entityClass->name)
            ->classId($classId)
            ->autoComplete([
                'manufacturer' => $entityClass->has_manufacturer,
                'location' => false,
                'supplier' => false
            ])
            ->feed());
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @param $classId
     * @return RedirectResponse|\Illuminate\Http\Response|\StdClass
     */
    public function store(Request $request, $classId)
    {

        $entityClass = EntityClass::get($classId, 1, true, true);

        $redirectBack = Entity::validateEntityChange($entityClass, $request, null, 1);

        if ($redirectBack) {
            Input::flash();
            return Redirect::back();
        }

        $entityId = null;

        DB::transaction(function () use ($request, $entityClass, $classId, &$entityId) {

            $entityId = DB::table('entities')
                ->insertGetId([
                    'entity_class_id' => $classId,
                    'location_id' => $entityClass->new_location_id, // Got already from validation
                    'manufacturer_id' => $entityClass->new_manufacturer_id, // Got already from validation
                    'supplier_id' => $entityClass->new_supplier_id, // Got already from validation
                    'created_at' => Carbon::now(),
                    'updated_at' => Carbon::now()
                ]);

            $parametersTable = DB::table('parameters');

            foreach ($entityClass->parameters as $parameter) {
                $desiredValue = $request->get('param_' . $parameter['name'], false);
                if ($desiredValue !== false)
                    $parametersTable
                        ->insert([
                            'entity_class_parameter_id' => $parameter['id'],
                            'entity_id' => $entityId,
                            $parameter['type'] => $desiredValue
                        ]);
            }
        });

        if ($entityId === null) {
            Session::flash('error', "Failed creating entity. Please see logs for more details");
            Input::flash();
            return Redirect::back();
        }

        return Redirect::route(self::getRouterName() . '.show', [
            'classId' => $classId,
            'id' => $entityId
        ]);
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @param $entityClassId
     * @return \Illuminate\Http\RedirectResponse
     */
    public function index(Request $request, $entityClassId)
    {
        $this->validate($request, [
            'ordering' => 'in:asc,desc',
            'count' => 'integer|min:5|max:100'
        ]);

        $entityClass = EntityClass::get($entityClassId, 1);

        if (!$entityClass) {
            Session::flash('error', "Entity class Id $entityClassId was not found.");
            return Redirect::route(self::getRouterName() . '.createDistinguish');
        }

        $this->validate($request, [
            'sort' => Rule::in(EntityClass::availableSorting($entityClass, null))
        ]);

        $sort = $request->get('sort', 'id');
        $ordering = $request->get('ordering', 'desc');

        list($columns, $entities) = Entity::getEntitiesWithEntityClass($entityClass, null, $sort, $ordering, $request);

        $viewFeeder = new ViewFeeder($this, ViewFeeder::LIST);

        return view('results.list', $viewFeeder
            ->results($entities)
            ->sorting($sort, $ordering)
            ->size(12)
            ->columns($columns)
            ->permissions([
                'canEdit' => Permission::EDIT_DEVICE_TYPE,
                'canDelete' => Permission::DELETE_DEVICE_TYPE,
            ])
            ->classId($entityClassId)
            ->heading('List of entities of class')
            ->plural($entityClass->name)
            ->feed()
        );
    }

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

        $entity = null;
        $columns = null;
        $entityClass = null;

        // Prevent anything to change our
        // Prevent anything to change between our selects
        DB::statement('set autocommit = 0');
        DB::statement('set transaction isolation level serializable;');
        DB::transaction(function () use ($entityClassId, $id, &$entity, &$columns, &$entityClass) {
            $entityClass = EntityClass::get($entityClassId, 1);

            if (!$entityClass) {
                Session::flash('error', "Entity type with class Id $entityClassId was not found, you can create new one here.");
                return Redirect::route(self::getRouterName() . '.create');
            }

            list($columns, $entity) = Entity::getEntityWithEntityClass($entityClass, null, $id);

            if (!$entity) {
                Session::flash('error', "Entity with id '$id' was not found");
                return Redirect::route(self::getRouterName() . '.index', [
                    'classId' => $entityClassId
                ]);
            }
        });


        $viewFeeder = new ViewFeeder($this, ViewFeeder::SHOW);

        return view('entity.show', $viewFeeder
            ->result($entity)
            ->columns($columns)
            ->classId($entityClassId)
            ->permissions([
                'canEdit' => Permission::EDIT_DEVICE,
                'canDelete' => Permission::DELETE_DEVICE
            ])
            ->size(6)
            ->singular($entityClass->name)
            ->feed());
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param $classId
     * @param $entityId
     * @return RedirectResponse|\Illuminate\Http\Response
     */
    public function edit($classId, $entityId)
    {
        $viewFeeder = new ViewFeeder($this, ViewFeeder::EDIT);

        $entityClass = EntityClass::get($classId, 1, true, true);

        if (!$entityClass) {
            Session::flash('error', "Entity Type with class id '$classId' not found.");
            return Redirect::route(self::getRouterName() . '.createDistinguish');
        }

        if ($entityClass instanceof RedirectResponse) {
            return $entityClass;
        }

        list($columns, $entity) = Entity::getEntityWithEntityClass($entityClass, null, $entityId);

        if (!$entity) {
            Session::flash('error', "Entity Type with id '$classId' not found.");
            return Redirect::route(self::getRouterName() . '.index', ['classId' => $classId]);
        }

        if ($entity instanceof RedirectResponse) {
            return $entity;
        }

        $entity->class = $entityClass;

        return view('entity.edit', $viewFeeder
            ->size(6)
            ->result($entity)
            ->singular($entityClass->name)
            ->columns($columns)
            ->classId($classId)
            ->autoComplete([
                'location' => false,
                'manufacturer' => $entityClass->has_manufacturer,
                'supplier' => false
            ])
            ->feed());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param EntityTypeRequest|Request $request
     * @param $classId
     * @param $entityId
     * @return RedirectResponse|\Illuminate\Http\Response
     */
    public function update(Request $request, $classId, $entityId)
    {
        $entityClass = EntityClass::get($classId, 1, true, false);

        $redirectBack = Entity::validateEntityChange($entityClass, $request, $entityId, 1);

        if ($redirectBack) {
            Input::flash();
            return Redirect::back();
        }

        DB::transaction(function () use ($request, $entityClass, $classId, $entityId) {

            $toUpdate = [
                'entity_class_id' => $classId,
                'manufacturer_id' => $entityClass->new_manufacturer_id, // Got already from validation
                'updated_at' => Carbon::now(),
            ];

            $finalState = $request->get('state_id', false);

            if ($finalState !== false)
                $toUpdate['state_id'] = $finalState;

            DB::table('entities')
                ->where('id', '=', $entityId)
                ->update($toUpdate);

            foreach ($entityClass->parameters as $parameter) {

                $desiredValue = $request->get('param_' . $parameter['name'], null);
                DB::table('parameters')
                    ->updateOrInsert(
                        [
                            'entity_class_parameter_id' => intval($parameter['id']),
                            'entity_id' => intval($entityId),
                        ],
                        [
                            $parameter['type'] => $desiredValue
                        ]);
            }
        });

        if ($entityId === null) {
            Session::flash('error', "Failed updating entity. Please see logs for more details");
            Input::flash();
            return Redirect::back();
        }

        return Redirect::route(self::getRouterName() . '.show', [
            'classId' => $classId,
            'id' => $entityId
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param $classId
     * @param $entityTypeId
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy($classId, $entityTypeId)
    {
        $hasChildren = DB::table('entities as d')
            ->join('entities as dt', 'd.entity_type_id', '=', 'dt.id')
            ->where([
                ['dt.id', '=', $entityTypeId]
            ])
            ->count();

        if ($hasChildren) {
            Session::flash('error', "Cannot remove this entity, because it has $hasChildren children. Remove it/them first.");
            return Redirect::back();
        }

        try {
            $deleted = DB::table('entities')
                ->delete($entityTypeId);
        } catch (QueryException $e) {
            Session::flash('error', $e->getMessage());
            return Redirect::back();
        }

        if ($deleted)
            Session::flash('success', Lang::trans('entity_type.delete_success', ['id' => $entityTypeId]));
        else
            Session::flash('error', Lang::trans('entity_type.delete_failed', ['id' => $entityTypeId]));

        return Redirect::back();
    }

    /**
     * Gets the name of the router to be used from the view.
     *
     * @return string
     */
    public static function getRouterName(): string
    {
        return 'entity-types';
    }

    /**
     * Returns singular of object controller is working with.
     *
     * @return string
     */
    public function getSingular(): string
    {
        return 'Entity Type';
    }

    /**
     * Returns plural of object controller is working with.
     *
     * @return string
     */
    public function getPlural(): string
    {
        return 'Entity Types';
    }

    /**
     * Returns array of columns to be shown when listing objects controller is working with.
     * @return array
     * @throws \Exception
     */
    public function getColumns(): array
    {
        return [];
    }

}
