<?php

namespace brain\api\v1;

use brain\data\Member;
use brain\data\Settings;

class ImagesAPI
{
    public function __construct()
    {
    }

    public static function uploadImage($request, $type = null)
    {
        $file = $request->getUploadedFiles();
        $uploadPath = "";
        $path = date("Y") . "/" . date("m");
        $response = [];
        switch ($type) {
            case "avatar":
                $image = $file["avatar_upload"];
                $uploadPath = "../public/assets/images/user/" . $path;
                break;
            case "background":
                $image = $file["background_upload"];
                $uploadPath = "../public/assets/images/user/" . $path;
                break;
            default:
                $image = $file["post_image"];
                $path = date("Y") . "/" . date("m");
                $uploadPath = "../public/assets/images/blog/" . $path;
                break;
        }

        $result = FileUploader::uploadFile($uploadPath, $image);

        switch ($type) {
            case "avatar":
                $response = [
                "message" => "Avatar Added. You look great!",
                "type" => "avatarUploaded",
                "url" =>
                "/assets/images/user/" . $path . "/" . $image->getClientFileName(),
                ];

              //update member data
                Member::updateData(
                    "avi",
                    "/assets/images/user/" . $path . "/" . $image->getClientFileName()
                );

                break;
            case "background":
                $response = [
                "message" => "Background plugged in. That's nice!",
                "type" => "siteBackgroundUploaded",
                "url" =>
                "/assets/images/user/" . $path . "/" . $image->getClientFileName(),
                ];

              //update settings file
                Settings::updateGlobalData(
                    "background",
                    "/assets/images/user/" . $path . "/" . $image->getClientFileName()
                );

                break;
            default:
                $response = [
                "message" => "Image Added. Very slick",
                "type" => "postImageAdded",
                "url" =>
                "/assets/images/blog/" . $path . "/" . $image->getClientFileName(),
                ];
                break;
        }

        return $response;
    }
}