<?php

namespace CodeClouds\Unify\Model;

/**
 * @package CodeClouds\Unify
 */
class ConfigEncryption
{

    /**
     * Add password salt
     *
     */
    public static function passwordEncrypt($key, $value, &$connection_metas)
    {

        $salt = \Codeclouds\Unify\Model\Protection\Salt::generate();
        $connection_metas['unify_connection_salt'] = $salt;

        $connection_metas[$key] = \Codeclouds\Unify\Model\Protection\Encryption::make((stripslashes($value)), $salt);

    }

    /**
     * Add other meta salt
     *
     */
    public static function metaEncrypt($key, $value, &$connection_metas)
    {

        $salt = \Codeclouds\Unify\Model\Protection\Salt::generate();
        $connection_metas[$key . '_salt'] = $salt;

        $connection_metas[$key] = \Codeclouds\Unify\Model\Protection\Encryption::make((stripslashes($value)), $salt);

    }

    /**
     * Decrypt password salt
     *
     */
    public static function passwordDecrypt($connection_detail, &$conn_data, $key)
    {

        $salt = get_post_meta($connection_detail['list'][0]['ID'], 'unify_connection_salt', true);
        $conn_data[$key] = \Codeclouds\Unify\Model\Protection\Decryption::make($connection_detail['list'][0]['unify_connection_api_password'], $salt);

    }

    /**
     * Decrypt other meta salt
     *
     */
    public static function metaDecrypt($connection_detail, &$conn_data, $key)
    {

        $salt = get_post_meta($connection_detail['list'][0]['ID'], $key . '_salt', true);

        $conn_data[$key] = \Codeclouds\Unify\Model\Protection\Decryption::make($connection_detail['list'][0][$key], $salt);

    }

    public static function metaDecryptSingle($data, $salt)
    {

        return (!empty($data) || !empty($salt)) ? \Codeclouds\Unify\Model\Protection\Decryption::make($data, $salt) : '';

    }

}
