<?php

namespace App\Controllers\Trait;

use CodeIgniter\HTTP\Files\UploadedFile;

trait UpDownloadTrait
{
    //Upload FIle관련
    private function upload_file_process(UploadedFile $upfile): ?string
    {
        $filename = null;
        $uploaded_filename = null;
        if ($upfile->isValid() && !$upfile->hasMoved()) {
            $filename = $upfile->getName();
            $uploaded_filename = $upfile->getRandomName();
            $upfile->move(PATHS['UPLOAD'], $uploaded_filename);
            //move시 중복된파일명이 있다면 파일명이 바뀌므로 여기서 한번더 파일명 확인 필요
            $uploaded_filename = $upfile->getName();
        } else {
            return null;
        }
        return $filename . DEFAULTS['DELIMITER_FILE'] . $uploaded_filename;
    }
    public function upload_file_procedure(string $field): ?string
    {
        return $this->upload_file_process($this->request->getFile($field));
    }
    public function upload_multiple_file_procedure(string $field): array
    {
        //Multiple파일의경우 html에서는 필드명[]를 넣어야하며
        //rule에서 "uploaded[필드명.0]|is_image[필드명]~~" 이런식으로 넣어야함
        $files = array();
        if ($upfiles = $this->request->getFiles()) {
            foreach ($upfiles[$field] as $upfile) {
                if ($upfile->isValid() && !$upfile->hasMoved()) {
                    $file = $this->upload_file_process($upfile);
                    if (!is_null($file)) {
                        array_push($files, $file);
                    }
                }
            }
        }
        return $files;
    }

    private function image_resize_process(UploadedFile $upfile, $uploaded_filename, $x = 25, $y = 25)
    {
        $image = \Config\Services::image();
        $image->withFile($upfile)
            ->resize($x, $y, true, 'height')
            ->save(PATHS['UPLOAD_IMAGE'] . $uploaded_filename);
    }
    private function upload_image_process(UploadedFile $upfile): ?string
    {
        //참고:https://www.positronx.io/codeigniter-resize-image-with-image-manipulation-tutorial/
        $filename = null;
        $uploaded_filename = null;
        if ($upfile->isValid() && !$upfile->hasMoved()) {
            $filename = $upfile->getName();
            $uploaded_filename = $upfile->getRandomName();
            $this->image_resize_process($upfile, "small_" . $uploaded_filename);
            $this->image_resize_process($upfile, "middle_" . $uploaded_filename, 50, 50);
            $this->image_resize_process($upfile, "large_" . $uploaded_filename, 100, 100);
            $upfile->move(PATHS['UPLOAD_IMAGE'], $uploaded_filename);
            //move시 중복된파일명이 있다면 파일명이 바뀌므로 여기서 한번더 파일명 확인 필요
            $uploaded_filename = $upfile->getName();
        } else {
            return null;
        }
        return $filename . DEFAULTS['DELIMITER_FILE'] . $uploaded_filename;
    }

    public function upload_image_procedure(string $field): ?string
    {
        return $this->upload_image_process($this->request->getFile($field));
    }
    public function upload_multiple_image_procedure(string $field): array
    {
        //Multiple파일의경우 html에서는 필드명[]를 넣어야하며
        //rule에서 "uploaded[필드명.0]|is_image[필드명]~~" 이런식으로 넣어야함
        $filenames = array();
        if ($upfiles = $this->request->getFiles()) {
            foreach ($upfiles[$field] as $upfile) {
                if ($upfile->isValid() && !$upfile->hasMoved()) {
                    $file = $this->upload_image_process($upfile);
                    if (!is_null($file)) {
                        array_push($files, $file);
                    }
                }
            }
        }
        return $filenames;
    }
}
