<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AUK;
use App\Models\Project;
use Illuminate\Support\Facades\Log;

class AUKController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);
        $top_auks = $project->auks()->where('parent_id', 0)->get();
        return view('auks.list', ['project' => $project, 'top_auks' => $top_auks]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);
        $top_auks = $project->auks()->where('parent_id', 0)->get();
        return view('auks.create', ['project' => $project, 'top_auks' => $top_auks]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $request->validate([
            'auk_name' => ['required', 'string', 'max:255'],
            'auk_path' => ['required', 'string', 'max:255'],
            'image_dir' => ['required', 'string', 'max:255'],
            'parent_id' => ['required', 'integer'],
        ]);

        // $user = 
        AUK::create([
            'project_id' => $project_id,
            'auk_name' => $request->auk_name,
            'auk_path' => $request->auk_path,
            'image_dir' => $request->image_dir,
            'parent_id' => $request->parent_id,
        ]);

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);
        $top_auks = $project->auks()->where('parent_id', 0)->get();
        $auk = AUK::find($id);
        return view('auks.edit', ['project' => $project, 'top_auks' => $top_auks, 'auk' => $auk]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $project_id = $request->session()->get('project_id');
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'number' => ['required', 'integer'],
            'parent_id' => 'required|integer|different:id',
        ]);

        $document = AUK::find($id);
        $document->project_id = $project_id;
        $document->name = $request->name;
        $document->number = $request->number;
        $document->parent_id = $request->parent_id;
        $document->save();

        return redirect()->route('auks.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        AUK::findOrFail($id)->delete();
        return redirect()->route('auks.index');
    }

    /**
     * Show form to to load AUKs from file system
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function scan(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);


        $auks = array();
        $MMC_PATH = $project->mmc_path;

        $mmc_dir = scandir($MMC_PATH);

        foreach ($mmc_dir as $entry) {
            $course_path = $MMC_PATH . '/' . $entry;
            if (is_dir($course_path)) {
                if (intval($entry) > 0 && intval($entry) < 99) {
                    $tmp_array = array();
                    $tmp_array['auk_name'] = "АУК " . $entry;
                    $tmp_array['auk_path'] = $entry;
                    $tmp_array['image_dir'] = "";
                    $mmc_course_dir = scandir($course_path);
                    foreach ($mmc_course_dir as $course_entry) {
                        if (strcasecmp($course_entry, 'images') == 0) {
                            $course_image_path = $course_path . '/' . $course_entry;
                            if (is_dir($course_image_path)) {
                                $tmp_array['image_dir'] = $course_entry;
                            }
                        }
                    }
                    array_push($auks, $tmp_array);
                }
            }
        }
        return view('auks.scan', ['project' => $project, 'auks' => $auks]);
    }

    /**
     * Load AUKs from file system
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function load(Request $request)
    {
        Log::alert("In load");
        $project_id = $request->session()->get('project_id');

        $i = 0;
        while ($request->input('auk_name_' . $i) !== null) {
            Log::alert("Loading " . $i);
            $auk_name = $request->input('auk_name_' . $i);
            $auk_path = $request->input('auk_path_' . $i);
            $image_dir = $request->input('image_dir_' . $i);
            Log::alert("auk_name \"" . $auk_name . "\"");
            Log::alert("auk_path \"" . $auk_path . "\"");
            Log::alert("image_dir \"" . $image_dir . "\"");

            AUK::create([
                'project_id' => $project_id,
                'auk_name' => $auk_name,
                'auk_path' => $auk_path,
                'image_dir' => $image_dir,
                'parent_id' => null,
            ]);
            $i++;
        }
        return redirect(route('auks.index'));
    }
}
