<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once '../database.php';
require '../../vendor/autoload.php';

if(!isset($_GET['supplier']))
{
    die('Specify Supplier');
}

use Google\Cloud\Translate\V2\TranslateClient;

$translate = new TranslateClient([
    'key' => 'AIzaSyBLs2Knj-NLLna0YDJyJr4F8NHl3-KVhJc'
]);

class Translate extends database
{
    public function getProducts($supplier)
    {
        $sql = 'SELECT * FROM tb_product WHERE cl_supplier=? AND cl_deleted_at IS NULL';
        return $this->callQuery($sql,[$supplier]);
    }
    
    public function updateSubCategory($bigSpecificationTable, $descriptionTable, $id)
    {
        if(!$this->fetchRow('tb_description', ['cl_product_id' => $id]))
        {
            $this->insert('tb_description', $descriptionTable);
        }
        else
        {
            $this->update('tb_description', $descriptionTable, ['cl_product_id' => $id]);
        }
        
        if(!$this->fetchRow('tb_big_specification', ['cl_product_id' => $id]))
        {
            $this->insert('tb_big_specification', $bigSpecificationTable);
        }
        else
        {
            $this->update('tb_big_specification', $bigSpecificationTable, ['cl_product_id' => $id]);
        }
        
       
    }
}
     
$supplier = $_GET['supplier'];

$obj = new Translate();
$products = $obj->getProducts($supplier);

$smallSpecificationTable = [];
$bigSpecificationTable = [];
$descriptionTable = [];
                                                
$count = 0;

foreach($products AS $product)
{
   
   if($product['cl_supplier'] == 'action' || $product['cl_supplier'] == 'makant' || $product['cl_supplier'] == 'difox')
   {
       $smallSpecificationTable = [];
        $bigSpecificationTable = [];
        $descriptionTable = [];
        
        $bigSpecificationTable['cl_product_id'] = $descriptionTable['cl_product_id'] = $product['cl_id'];
        
        
        
        /*if($product['cl_small_specification'] != null || trim($product['cl_small_specification']) != '')
        {
            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'en',
                'target' => 'lv'
            ]);
            $smallSpecificationTable['cl_lv'] = $result['text'];

            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'en',
                'target' => 'lt'
            ]);
            $smallSpecificationTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'en',
                'target' => 'ee'
            ]);
            $smallSpecificationTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'en',
                'target' => 'fi'
            ]);
            $smallSpecificationTable['cl_fi'] = $result['text'];
            $smallSpecificationTable['cl_en'] = $product['cl_small_specification'];
        }*/
        
        
        if($product['cl_big_specification'] != null || trim($product['cl_big_specification']) != '')
        {
        
            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'en',
                'target' => 'lv'
            ]);
            $bigSpecificationTable['cl_lv'] = $result['text'];

            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'en',
                'target' => 'lt'
            ]);
            $bigSpecificationTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'en',
                'target' => 'ee'
            ]);
            $bigSpecificationTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'en',
                'target' => 'fi'
            ]);
            $bigSpecificationTable['cl_fi'] = $result['text'];
            $bigSpecificationTable['cl_en'] = $product['cl_big_specification'];
        }
        
        
        
        if($product['cl_description'] != null || trim($product['cl_description']) != '')
        {
        
            $result = $translate->translate($product['cl_description'], [
                'source' => 'en',
                'target' => 'lv'
            ]);
            $descriptionTable['cl_lv'] = $result['text'];

            $result = $translate->translate($product['cl_description'], [
                'source' => 'en',
                'target' => 'lt'
            ]);
            $descriptionTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_description'], [
                'source' => 'en',
                'target' => 'ee'
            ]);
            $descriptionTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_description'], [
                'source' => 'en',
                'target' => 'fi'
            ]);
            $descriptionTable['cl_fi'] = $result['text'];
            $descriptionTable['cl_en'] = $product['cl_description'];
        }

        
   }
   else if($product['cl_supplier'] == 'abpl')
   {
        $smallSpecificationTable = [];
        $bigSpecificationTable = [];
        $descriptionTable = [];
        
        $bigSpecificationTable['cl_product_id'] = $descriptionTable['cl_product_id'] = $product['cl_id'];
        
       /* if($product['cl_small_specification'] != null || trim($product['cl_small_specification']) != '')
        {
            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'pl',
                'target' => 'lv'
            ]);
            $smallSpecificationTable['cl_lv'] = $result['text'];

            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'pl',
                'target' => 'lt'
            ]);
            $smallSpecificationTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'pl',
                'target' => 'ee'
            ]);
            $smallSpecificationTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'pl',
                'target' => 'fi'
            ]);
            $smallSpecificationTable['cl_fi'] = $result['text'];

            $result = $translate->translate($product['cl_small_specification'], [
                'source' => 'pl',
                'target' => 'en'
            ]);
            $smallSpecificationTable['cl_en'] = $result['text'];
        }*/
        
        
        if($product['cl_big_specification'] != null || trim($product['cl_big_specification']) != '')
        {
        
            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'pl',
                'target' => 'lv'
            ]);
            $bigSpecificationTable['cl_lv'] = $result['text'];

            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'pl',
                'target' => 'lt'
            ]);
            $bigSpecificationTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'pl',
                'target' => 'ee'
            ]);
            $bigSpecificationTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'pl',
                'target' => 'fi'
            ]);
            $bigSpecificationTable['cl_fi'] = $result['text'];

            $result = $translate->translate($product['cl_big_specification'], [
                'source' => 'pl',
                'target' => 'en'
            ]);
            $bigSpecificationTable['cl_en'] = $result['text'];
        }
        
        
        
        if($product['cl_description'] != null || trim($product['cl_description']) != '')
        {
        
            $result = $translate->translate($product['cl_description'], [
                'source' => 'pl',
                'target' => 'lv'
            ]);
            $descriptionTable['cl_lv'] = $result['text'];

            $result = $translate->translate($product['cl_description'], [
                'source' => 'pl',
                'target' => 'lt'
            ]);
            $descriptionTable['cl_lt'] = $result['text'];

            $result = $translate->translate($product['cl_description'], [
                'source' => 'pl',
                'target' => 'ee'
            ]);
            $descriptionTable['cl_ee'] = $result['text'];

            $result = $translate->translate($product['cl_description'], [
                'source' => 'pl',
                'target' => 'fi'
            ]);
            $descriptionTable['cl_fi'] = $result['text'];

            $result = $translate->translate($product['cl_description'], [
                'source' => 'pl',
                'target' => 'en'
            ]);
            $descriptionTable['cl_en'] = $result['text'];
        }

        
   }
        
    $obj->updateSubCategory($bigSpecificationTable, $descriptionTable, $product['cl_id']);
    
}
