<?php

namespace Tops\AmazonSellingPartnerAPI\Helpers;

trait FeedHelper
{
     /*
    @Description    : Function to set access token
    @Author         : Harshad Hirapara
    @Input          : access token
    @Output         : set access token
    @Date           : 12-04-2021
    */
    public function uploadFeedDocument($payload, $feedContentFilePath,$header)
    {      
        $encryptionDetails = $payload['encryptionDetails'];
        $feedUploadUrl = $payload['url'];

        $key = $encryptionDetails['key'];
        $initializationVector = $encryptionDetails['initializationVector'];

        // base64 decode before using in encryption
        $initializationVector = base64_decode($initializationVector, true);
        $key = base64_decode($key, true);

        // get file to upload
        $file = file_get_contents($feedContentFilePath);
    
        $fileResourceType = gettype($file);

        // resource or string ? make it to a string
        if ($fileResourceType == 'resource') {
            $file = stream_get_contents($file);
        }

        // utf8 !
        $file = utf8_encode($file);

        // encrypt string and get value as base64 encoded string
        $encryptedFile = $this->encrypt($file, $key, $initializationVector);

        $ch = curl_init();      
        curl_setopt($ch, CURLOPT_URL, $feedUploadUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $encryptedFile);
        $result = curl_exec($ch);        
       
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        return $httpcode;
    }

    public function encrypt($plainText, $key, $iv)
    {   
        $stringLength = strlen($plainText);
        
        if ($stringLength % 8) 
        {
            $plainText = str_pad($plainText, $stringLength + 8 - $stringLength % 8, "\0");
        }

        return openssl_encrypt($plainText, "AES256", $key, OPENSSL_RAW_DATA, $iv);
    }

     /*
    @Description    : Function to download feed processing report
    @Author         : Harshad Hirapara
    @Input          : payload
    @Output         : report data
    @Date           : 12-04-2021
    */

    public function downloadFeedDocument($payload)
    {
        $encryptionDetails = $payload['encryptionDetails'];
        $feedDownloadUrl = $payload['url'];

        $key = $encryptionDetails['key'];
        $initializationVector = $encryptionDetails['initializationVector'];

        // base64 decode before using in encryption
        $initializationVector = base64_decode($initializationVector, true);
        $key = base64_decode($key, true);

        $decryptedFile = $this->decrypt(file_get_contents($feedDownloadUrl), $key, $initializationVector);

        if($payload['compressionAlgorithm']=='GZIP') 
        {
            $decryptedFile=gzdecode($decryptedFile);
        }

        $decryptedFile = preg_replace('/\s+/S', " ", $decryptedFile);

        return $decryptedFile;
    }

    /*
    @Description    : Function to download feed processing report
    @Author         : Harshad Hirapara
    @Input          : payload
    @Output         : report data
    @Date           : 12-04-2021
    */
    public function decrypt($encryptedText, $key,$iv)
    {
        return openssl_decrypt($encryptedText, "AES256", $key, OPENSSL_RAW_DATA, $iv);
    }

    /*
    @Description    : Function to upload feed document
    @Author         : Jignesh Kapadia
    @Date           : 31-05-2023
    */
    public function uploadFeedDocument_New($payload, $feedContentFilePath, $content_type="application/json")
    {      
        $feedUploadUrl = $payload['url'];

        // get file to upload
        $file = file_get_contents($feedContentFilePath);
    
        $fileResourceType = gettype($file);

        // resource or string ? make it to a string
        if ($fileResourceType == 'resource') {
            $file = stream_get_contents($file);
        }

        // utf8 !
        $file = utf8_encode($file);

        $ch = curl_init();      
        curl_setopt($ch, CURLOPT_URL, $feedUploadUrl);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT");
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: ".$content_type));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
        $result = curl_exec($ch);        
       
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        if ($httpcode == 200) {
            return 'Done';
        } else {
            return curl_error($ch);
        }
    }

    /*
    @Description    : Function to download feed report
    @Author         : Jignesh Kapadia
    @Date           : 31-05-2023
    */
    public function downloadFeedDocument_New($payload)
    {
        $feedDownloadUrl = $payload['url'];

        $data = file_get_contents($feedDownloadUrl);

        if(isset($payload['compressionAlgorithm']) && $payload['compressionAlgorithm']=='GZIP') 
        {
            $data=gzdecode($data);
        }

        return $data;
    }
}