<?php
namespace App\Models;

use CodeIgniter\Model;

class UploadManager extends Model
{
    public $savePath = WRENCHBOARD_UPLOAD_PATH; //"/opt/wrenchboard/TEST/";
    public function reciveUpload($in,$out){
        $this->savePath =  $in["system_primary_uploads"];
        $action = $in["action"];


        //DECIDE IF PARENT FOLDERS ARE PRESENT
        if (!file_exists($this->savePath.'CONTRACTS')) {
             mkdir($this->savePath.'CONTRACTS', 0777, true);
        }

        if (!file_exists($this->savePath.'PROFILE')) {
            mkdir($this->savePath.'PROFILE', 0777, true);
        }

        if (!file_exists($this->savePath.'FAMILY')) {
            mkdir($this->savePath.'FAMILY', 0777, true);
        }

        if (!file_exists($this->savePath.'JOBS')) {
            mkdir($this->savePath.'JOBS', 0777, true);
        }

        log_message('critical', "UploadManager **********  UPLOAD-> " );

        $out= $in;

        switch ($action){

            case WRENCHBOARD_PICTURE_FAMBANNER:

                if ( trim($in["uid"]) == ''){
                    $out["error_msg"] = "invalid_upload_call";
                    $out["call_backend"] = false; // something went bad
                    return -1;
                }
                // MAKE SURE FOLDER AS PER PROCESS IS AVAILABLE
                $thisProfileFolder = $this->savePath.'PROFILE/'.$in["uid"];
                if (!file_exists($thisProfileFolder)) {
                    mkdir($thisProfileFolder, 0777, true);
                }
                if (!file_exists($thisProfileFolder."/FAMBANNER")) {
                    mkdir($thisProfileFolder."/FAMBANNER", 0777, true);
                }

                $myFilesFolder = $thisProfileFolder."/FAMBANNER";

                if (file_exists($myFilesFolder)) {
                    //$tt_d = date('hisjmy');
                    $out["message"] = "completed";
                    $pieces = explode(".",  $in['file_name']);
                    $in['profile'] = "profile.".$pieces[1];
                    $myFilesFolderFileName =  $myFilesFolder.'/'. $in['profile'];
                    $myfile = fopen($myFilesFolderFileName, "w") or die("Unable to open file!");
                    $file_data = base64_decode($in["file_data"]);
                    fwrite($myfile, $file_data);
                    fclose($myfile);
                    $out["call_backend"] = false; // flag ready to save data entry
                    $data['uid'] =$in["uid"];
                    $data['sessionid'] =$in["sessionid"];
                    $data['profile'] = $in["profile"];
                    $data['member_id'] = $in["member_id"];
                    $data['file_section'] = WRENCH_FILE_FAMILYBANNER;
                    $out['upload_result'] = $this->specififBackendCall(WRENCHBOARD_SAVE_MEDIA,$data);
                    /*
                     *     public function specififBackendCall($action,$data){
        $in["action"] = $action;
        $wrenchboard = new  \App\Models\BackendModel();
        $ret = $wrenchboard->wrenchboard_api($data, $out);
        return $ret;
    }
                     */
                }
                break;

            case WRENCHBOARD_PICTURE_PROFILE:

                if ( trim($in["uid"]) == ''){
                    $out["error_msg"] = "invalid_upload_call";
                    $out["call_backend"] = false; // something went bad
                    return -1;
                }
                // MAKE SURE FOLDER AS PER PROCESS IS AVAILABLE
                $thisProfileFolder = $this->savePath.'PROFILE/'.$in["uid"];
                if (!file_exists($thisProfileFolder)) {
                    mkdir($thisProfileFolder, 0777, true);
                }
                if (!file_exists($thisProfileFolder."/PORTRAIT")) {
                    mkdir($thisProfileFolder."/PORTRAIT", 0777, true);
                }

                $myFilesFolder = $thisProfileFolder."/PORTRAIT";

                if (file_exists($myFilesFolder)) {
                    //$tt_d = date('hisjmy');
                    $out["message"] = "completed";
                    $pieces = explode(".",  $in['file_name']);
                    $in['profile'] = "profile.".$pieces[1];
                    $out['profile_file_name'] = $in['profile']; // this is the final filename
                    $myFilesFolderFileName =  $myFilesFolder.'/'. $in['profile'];
                    $myfile = fopen($myFilesFolderFileName, "w") or die("Unable to open file!");
                    $file_data = base64_decode($in["file_data"]);
                    fwrite($myfile, $file_data);
                    fclose($myfile);
                    $out["call_backend"] = false; // flag ready to save data entry
                    $data['uid'] =$in["uid"];
                    $data['sessionid'] =$in["sessionid"];
                    $data['profile'] = $in["profile"];
                    $data['member_id'] = $in["member_id"];
                    $data['file_section'] = WRENCH_FILE_PROFILE;
                    $out['upload_result'] = $this->specififBackendCall(WRENCHBOARD_SAVE_MEDIA,$data);
                }
                break;

            case WRENCHBOARD_PICTURE_JOB:

                if ( trim($in["uid"]) == ''){
                    $out["error_msg"] = "invalid_upload_call";
                    $out["call_backend"] = false; // something went bad
                    return -1;
                }
                // MAKE SURE FOLDER AS PER PROCESS IS AVAILABLE
                $thisProfileFolder = $this->savePath.'PROFILE/'.$in["uid"];
                if (!file_exists($thisProfileFolder)) {
                    mkdir($thisProfileFolder, 0777, true);
                }
                if (!file_exists($thisProfileFolder."/JOBS")) {
                    mkdir($thisProfileFolder."/JOBS", 0777, true);
                }

                $myFilesFolder = $thisProfileFolder."/JOBS";

                if (file_exists($myFilesFolder)) {
                    //$tt_d = date('hisjmy');
                    $out["message"] = "completed";
                   // $pieces = explode(".",  $in['file_name']);
                    $tt_d = date('hisjmy');
                    $in['profile'] = $tt_d ."-". $in['file_name'];
                    $myFilesFolderFileName =  $myFilesFolder.'/'. $in['profile'];
                    $myfile = fopen($myFilesFolderFileName, "w") or die("Unable to open file!");
                    $file_data = base64_decode($in["file_data"]);
                    fwrite($myfile, $file_data);
                    fclose($myfile);
                    $out["call_backend"] = false; // flag ready to save data entry
                    $data['uid'] =$in["uid"];
                    $data['job_uid'] =$in["job_uid"];
                    $data['sessionid'] =$in["sessionid"];
                    $data['profile'] = $in["profile"];
                    $data['member_id'] = $in["member_id"];
                    $data['file_section'] = WRENCH_FILE_JOB;
                    $out['upload_result'] = $this->specififBackendCall(WRENCHBOARD_SAVE_MEDIA,$data);
                }
                break;

            case WRENCHBOARD_PICTURE_FAMMEMBER:
                if ( trim($in["uid"]) == '' && trim($in["family_uid"]) == ''){
                    $out["error_msg"] = "invalid_upload_call";
                    $out["call_backend"] = false; // something went bad
                    return -1;
                }
                // MAKE SURE FOLDER AS PER PROCESS IS AVAILABLE
                $thisProfileFolder = $this->savePath.'PROFILE/'.$in["uid"];
                if (!file_exists($thisProfileFolder)) {
                    mkdir($thisProfileFolder, 0777, true);
                }
                if (!file_exists($thisProfileFolder."/FAMILY")) {
                    mkdir($thisProfileFolder."/FAMILY", 0777, true);
                }

                if (!file_exists($thisProfileFolder."/FAMILY/".$in["family_uid"] )) {
                    mkdir($thisProfileFolder."/FAMILY/".$in["family_uid"], 0777, true);
                }


               $myFilesFolder = $thisProfileFolder."/FAMILY/".$in["family_uid"];

                if (file_exists($myFilesFolder)) {
                    $out["message"] = "completed";
                    $pieces = explode(".", $in['file_name']);
                    $in['profile'] = "profile." . $pieces[1];
                    $myFilesFolderFileName = $myFilesFolder . '/' . $in['profile'];
                    $myfile = fopen($myFilesFolderFileName, "w") or die("Unable to open file!");
                    $file_data = base64_decode($in["file_data"]);
                    fwrite($myfile, $file_data);
                    fclose($myfile);
                    $out["call_backend"] = false; // flag ready to save data entry
                    $data['uid'] = $in["uid"];
                    $data['profile'] = $in["profile"];
                    $data['family_uid'] =$in["family_uid"];
                    $data['sessionid'] =$in["sessionid"];
                    $data['member_id'] = $in["member_id"];
                    $data['file_section'] = WRENCH_FILE_FAMILY;
                    $out['upload_result'] = $this->specififBackendCall(WRENCHBOARD_SAVE_MEDIA, $data);
                }
                break;

            case WRENCHBOARD_RESOURCE_MYFILES:

                if ( trim($in["uid"]) == ''){
                    $out["error_msg"] = "invalid_upload_call";
                    $out["call_backend"] = false; // something went bad
                    return -1;
                }
                // MAKE SURE FOLDER AS PER PROCESS IS AVAILABLE
                $thisProfileFolder = $this->savePath.'PROFILE/'.$in["uid"];
                if (!file_exists($thisProfileFolder)) {
                    mkdir($thisProfileFolder, 0777, true);
                }
                if (!file_exists($thisProfileFolder."/MYFILES")) {
                    mkdir($thisProfileFolder."/MYFILES", 0777, true);
                }

                $myFilesFolder = $thisProfileFolder."/MYFILES";

                if (file_exists($myFilesFolder)) {
                    $tt_d = date('hisjmy');
                    $out["message"] = $tt_d."-".$in['file_name'];
                    $myFilesFolderFileName =  $myFilesFolder.'/'.$out["message"];
                    $myfile = fopen($myFilesFolderFileName, "w") or die("Unable to open file!");
                    $file_data = base64_decode($in["file_data"]);
                    fwrite($myfile, $file_data);
                    fclose($myfile);
                    $out["call_backend"] = true; // flag ready to save data entry
                }
                break;

            case WRENCHBOARD_CONTRACT_MESSAGE:
                    // MAKE SURE FOLDER AS PER PROCESS IS AVAILABLE
                    $thisContactFolder = $this->savePath.'CONTRACTS/'.$in["contract"];
                    if (!file_exists($thisContactFolder)) {
                        mkdir($thisContactFolder, 0777, true);
                    }

                    if (file_exists($thisContactFolder)) {
                        $tt_d = date('hisjmy');
                        $out["message"] = $tt_d."-".$in['file_name'];
                        $thisContactFolderFileName =  $thisContactFolder.'/'.$out["message"];
                        $myfile = fopen($thisContactFolderFileName, "w") or die("Unable to open file!");
                        $file_data = base64_decode($in["file_data"]);
                        fwrite($myfile, $file_data);
                        fclose($myfile);
                        $out["call_backend"] = true; // flag ready to save data entry
                    }
                break;

        }


        return $out;
    }

    public function specififBackendCall($action,$data){
        log_message('critical', "UploadManager specififBackendCall **********  UPLOAD-> " );
        $data["action"] = $action;
        $data["file_data"] =''; // dont send file data ever
        $wrenchboard = new  \App\Models\BackendModel();
        $ret = $wrenchboard->wrenchboard_api($data, $out);
        return $ret;
    }
}

/*
 *
 * 'message' => string 'Lorem Ipsum comes from a latin text written in 45BC by Roman statesman, lawyer, scholar, and philosopher, Marcus Tullius Cicero' (length=127)
  'msg_type' => string
 * 'member_id' => int 1
  'sessionid' => string '9739747631A235F6C316F1BC8C7D823D873B5DAEB7922750AECFE1E887E4D3A9' (length=64)
  'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  'file_name' => string 'international-user.svg' (length=22)
  'file_size' => int 1421
  'file_type' => string 'image/svg+xml' (length=13)
  'file_data' => string 'PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0OCA0OCIgaWQ9IkludGVybmF0aW9uYWxVc2VyIj48cGF0aCBmaWxsPSIjZTZlN2Y5IiBkPSJNMzguNSAzMS4xYzIuMS0xLjEgMy45LTIuNSA1LjQtNC40aC0zLjJjLS42IDEuNi0xLjMgMy4xLTIuMiA0LjR6bS01LjggMS40YzItLjcgMy43LTIuOCA0LjktNS44aC00Ljl2NS44em04LjgtOC42aDQuMmMuOS0xLjggMS41LTMuNyAxLjctNS44aC01LjFjLS4xIDItLjQgNC0uOCA1Ljh6bTIuNC0xNy4zYy0xLjUtMS44LTMuMy0zLjMtNS40LTQuNC44IDEuMiAxLjYgMi43IDIuMiA0LjRoMy4yem0tNS4zIDIuOWgtNS45djUuOGg2LjdjLS4xLTIuMS0uNC00LjEtLjgtNS44em0zLjcgNS44'... (length=1896)
  'msg_type' => string 'FILE' (length=4)
  'contract' => string '49BWBRW738' (length=10)
  'action' => int 14010
/var/www/html/public/wrenchboard/uploads.php:48:null
 */
