<?php

namespace App\Services;

class UploadMedia
{
    private $user_request = null;

    public function __construct($request)
    {
        $this->user_request = $request;
    }

    public function uploadmedia()
    {

        $auth_id = auth()->id();
        if (isset($this->user_request['uploadmedia']['identification'])) {
            $rtg_letter_image_id = 1;
            $image_path = 'identification';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['mail_doc'])) {
            $rtg_letter_image_id = 2;
            $image_path = 'mail_doc';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['ssn'])) {
            $rtg_letter_image_id = 3;
            $image_path = 'ssn';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['state_photo_id'])) {
            $rtg_letter_image_id = 4;
            $image_path = 'state_photo_id';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['notarized_letter'])) {
            $rtg_letter_image_id = 5;
            $image_path = 'notarized_letter';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['cr_report'])) {
            $rtg_letter_image_id = 6;
            $image_path = 'cr_report';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['trouble_mark_file'])) {
            $rtg_letter_image_id = 7;
            $image_path = 'trouble_mark_file';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['item_challenge_file'])) {
            $rtg_letter_image_id = 8;
            $image_path = 'item_challenge_file';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

        if (isset($this->user_request['uploadmedia']['profile_pic'])) {
            $rtg_letter_image_id = 9;
            $image_path = 'profile_pic';
            $this->uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id);
        }

    }

    private function uploadOrupdateMeadiaFile($image_path, $auth_id, $rtg_letter_image_id)
    {

        if (!empty($this->user_request->file('uploadmedia.'.$image_path))) {
            $uploadMediaObj = app(\App\Models\Uploadmedia::class);
            $clasue['user_id'] = $auth_id;
            $clasue['image_type_id'] = $rtg_letter_image_id;
            $uploadmedia = $uploadMediaObj->where($clasue)->first();
            //$this->user_request['uploadmedia'][$image_path]
            $file = $this->user_request->file('uploadmedia.'.$image_path);
            $image_data = imageFileRotate($this->user_request->file('uploadmedia.'.$image_path));

            $file_name = time()."_".removeAnySpaces($file->getClientOriginalName(),'_');

            $file_path = config('constant.UPLOAD_MEDIA_PATH').DIRECTORY_SEPARATOR.$auth_id;

            if (empty($uploadmedia)) {
                $uploadMediaObj->create([
                    'user_id'=>$auth_id,
                    'image_type_id'=>$rtg_letter_image_id,
                    'image_path'=>$file_name
                ]);
                uploadFile($file_path,$file_name,$image_data);
            } else {
                deleteFile($file_path.DIRECTORY_SEPARATOR.$uploadmedia->image_path);
                uploadFile($file_path,$file_name,$image_data);
                $uploadmedia->update(['image_path'=>$file_name]);
            }


        }
    }

}
