<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Project;

class MMCController 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);
        return view('mmc.list', ['project' => $project]);
    }

    /**
     * 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);
        return view('mmc.create', ['project' => $project]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $request->validate([
        //     'username' => ['required', 'string', 'max:255'],
        //     'name' => ['required', 'string', 'max:255'],
        //     'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        //     'password' => ['required', 'confirmed', Rules\Password::defaults()],
        // ]);

        // // $user = 
        // MMC::create([
        //     'username' => $request->username,
        //     'name' => $request->name,
        //     'email' => $request->email,
        //     'is_admin' => false,
        // ]);

        // return redirect(route('mmc.index',$project_id));
    }

    /**
     * Display the specified resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $id)
    {
        // $user = User::find($id);
        // return view('admin.users.show', ['user' => $user]);
    }

    /**
     * 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)
    {
        // $user = User::find($id);
        // return view('admin.users.edit', ['user' => $user]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // $request->validate([
        //     'username' => 'required|string|max:255|unique:users,username,' . $id,
        //     'name' => 'required|string|max:255',
        //     'email' => 'required|email|unique:users,email,' . $id,
        // ]);

        // $user = User::find($id);

        // $user->username = $request->username;
        // $user->name = $request->name;
        // $user->email = $request->email;
        // $user->save();

        // return redirect(route('users.index'));
    }

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

    public function scan(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);


        $MMC = array();
        $total_size = 0;
        $total_count = 0;
        $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) {
                    // $MMC[] = $entry;
                    // echo ($entry . "\n");
                    $mmc_course_dir = scandir($course_path);
                    foreach ($mmc_course_dir as $course_entry) {
                        // VVV Images VVV
                        if (strcasecmp($course_entry, 'images') == 0) {
                            $course_image_path = $course_path . '/' . $course_entry;
                            if (is_dir($course_image_path)) {
                                // echo ($course_image_path . "\n");
                                $MMC[$entry]['image_dir'] = $course_entry;
                                $course_images = scandir($course_image_path);
                                foreach ($course_images as $image) {
                                    $image_file_path = $course_image_path . '/' . $image;
                                    if (is_file($image_file_path)) {
                                        if (strncmp($entry, $image, 2) == 0) {
                                            if (
                                                strcasecmp(substr($image, -3, 3), 'jpg') == 0 ||
                                                strcasecmp(substr($image, -3, 3), 'svg') == 0 ||
                                                strcasecmp(substr($image, -3, 3), 'swf') == 0
                                            ) {
                                                // $MMC[$entry][] = $image;
                                                $tmp_array = array();
                                                $tmp_array['file'] = $image;
                                                $tmp_array['size'] = filesize($image_file_path);
                                                $tmp_array['mtime'] = filemtime($image_file_path);
                                                $tmp_array['atime'] = fileatime($image_file_path);

                                                $MMC[$entry]['images'][] = $tmp_array;

                                                $total_size += $tmp_array['size'];
                                                $total_count++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        // ^^^ Images ^^^
                    }
                }
            }
        }

        return view('mmc.scan', ['project' => $project, 'MMC' => $MMC]);
    }
}
