<?php

namespace App\Http\Controllers;

use App\Imports\UsersImport;
use App\Models\AvaliadorPad;
use App\Models\Pad;
use App\Models\User;
use App\Models\UserType;
use App\Models\Util\MaskHelper;
use App\Models\Util\Menu;
use App\Models\Util\Status;
use App\Search\UserSearch;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use Maatwebsite\Excel\HeadingRowImport;
use NunoMaduro\Collision\Adapters\Phpunit\State;

class UserController extends Controller
{
    public function editPerfil($tab = null)
    {   
        return view('user.update_perfil', [
            'tab' => $tab,
            'menu' => Menu::USER,
        ]);
    }

    public function updatePerfil(Request $request)
    {   
        $user_id = Auth::user()->id;
        $validator = User::validator($request->all(), $user_id);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors());
        }

        $user = User::find($user_id);
        $user->fill($request->all());
        $user->document = $user->document !== null ? MaskHelper::documentOnlyNumber($user->document) : '';
        $user->save();

        return redirect()->route('edit_perfil')->with('success', 'Salvo com sucesso!');
    }

    public function updatePassword(Request $request)
    {
        $validator = User::validatorPassword($request->all());

        if($validator->fails()) {
            return redirect()->route('edit_perfil', ['tab' => 'password'])->withErrors($validator->errors());
        }

        $model = User::find(Auth::user()->id);
        $model->password = Hash::make($request->password);
        $model->save();

        return redirect()->route('edit_perfil', ['tab' => 'user'])->with('success', 'Senha salva com sucesso!');

    }

    public function actionIndex(Request $request)
    {
        $model_search = new UserSearch();
        
        $users = $model_search->search($request->all());

        return view('users.index', [
            'model_search' => $model_search, 'users' => $users, 'menu' => Menu::USERS,
        ]);
    }

    // Admin
    public function actionCreate()
    {   
        $model = new User();

        return view('users.create', [
            'menu' => Menu::USERS,
            'model' => $model,
            'tab' => 'user',
        ]);
    }

    public function actionStore(Request $request)
    {   
        $validator = User::validator($request->all(), null, true, ['curso_id', 'campus_id']);

        if($validator->fails()) {
            return redirect()->route('user_create')->withErrors($validator)->withInput();
        }

        $model = new User();
        $model->fill($request->all());
        $model->status = Status::ATIVO;

        $email_splited = explode('@', $model->email);
        $password = array_shift($email_splited);
        $model->password = Hash::make($password);

        if($model->save()) {
            return redirect()->route('user_edit', ['id' => $model->id])->with('success', 'Usuário cadastrado com sucesso!');
        }

        return redirect()->with('fail', 'Falha ao cadastrar Usuário!');
    }

    public function actionEdit(Request $request, $id)
    {   
        $model = User::find($id);
        $profiles = $model->profiles;
        $status = [
            Status::ATIVO => Status::listStatus(Status::ATIVO),
            Status::INATIVO => Status::listStatus(Status::INATIVO)
        ];

        if(count($profiles) <= 0) {
            Session::flash('warning', 'Não existem papeis cadastrados para esse usuário!');
        }

        $tab = $request->query('tab') ?? 'user';

        return view('users.update', [
            'menu' => Menu::USERS,
            'model' => $model,
            'status' => $status,
            'profiles' => $profiles,
            'tab' => $tab
        ]);
    }

    public function actionUpdate(Request $request, $id)
    {   
        $model = User::find($id);

        $validator = User::validator($request->all(), $model->id, true, ['curso_id', 'campus_id']);

        if($validator->fails()) {
            return redirect()->route('user_edit', ['id' => $model->id])->withErrors($validator)->withInput();
        }

        $model->fill($request->all());

        if($model->save()) {
            return redirect()->route('user_index')->with('success', 'Usuário atualizado com sucesso!');
        }

        return redirect()->with('fail', 'Falha ao cadastrar Usuário!');
    }

    public function actionDelete($id)
    {
        dd($id);
    }

    public function actionChangeProfile($user_id, $user_type_id)
    {
        $profiles = UserType::whereUserId($user_id)->get();
        foreach($profiles as $profile) {
            $profile->selected = false;
            $profile->status = UserType::STATUS_ATIVO;
            $profile->save();
        }

        $profile = UserType::whereId($user_type_id)->first();
        $profile->selected = true;
        $profile->status = UserType::STATUS_ATIVO;
        $profile->save();

        return redirect('dashboard');
    }

    public function actionImport(Request $request)
    {   
        $request->validate(['file' => 'required|mimes:csv,txt,xlx,xls,xlsx,pdf|max:2048']);
        
        $file = $request->file;
        	
        $excel = Excel::toArray(new UsersImport, $file)[0];

        unset($excel[0]);

        $excel = array_filter($excel,
            function($row) {
                $email = trim($row[7]);
                return strstr($email, '@');
            }
        );

        $importCount = 0;
        $duplicados = 0;
        foreach($excel as $row)
        {   
            $email = trim(strtolower($row[7]));

            $userQuery = User::initQuery()->whereEmail($email)->first();
            
            if($userQuery === null) {

                $password = explode("@", $email);
                $password = array_shift($password);

                $user = new User();
                $user->name = trim($row[1]);
                $user->email = $email;
                $user->password = Hash::make($password);
                $user->status = Status::ATIVO;

                if($user->save())
                {
                    $userType = new UserType();
                    $userType->user_id = $user->id;
                    $userType->type = UserTYPE::TEACHER;
                    $userType->status = Status::ATIVO;
                    $userType->selected = true;

                    if($userType->save())
                    {
                        $importCount++;
                    }
                }

            } else {
                $duplicados++;
            }
        }

        return redirect()->route('user_import_view')->with('success', sprintf('Importado com Sucesso! Importados: %d | Duplicados: %d', $importCount, $duplicados));
    }

    public function actionImportView()
    {
        return view('users.importView');   
    }

    /**
     *
     * @params Illuminate\Http\Request\Request
     */
    public function actionSearch(Request $request)
    {   
        // QueryParams
        $q = $request->query('q'); 
        $id = $request->query('id');
        $remove_avaliators_by_pad_id = $request->query('remove_avaliators_by_pad_id');

        $user = User::where([]);

        if($id) {
            $user = $user->whereId($id);
        }

        if($q) {
            $user = $user->where('name', 'like', '%'.$q.'%');
        }

        if($remove_avaliators_by_pad_id) {
            $avaliadorPad = AvaliadorPad::wherePadId($remove_avaliators_by_pad_id);
            $avaliadorPadIds = $avaliadorPad->get()->pluck('user_id')->toArray();

            $user = $user->whereNotIn('id', $avaliadorPadIds);
        }

        $users = $user->get();

        $array = 
            $users->map(function($user, $key)
            {
                return [
                    'id' => $user->id,
                    'text' => $user->name,
                ];
            });

        $array = ['results' => $array];

        return Response::json($array);
    }

}
