<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Payments Model
 *
 * @method \App\Model\Entity\Payment get($primaryKey, $options = [])
 * @method \App\Model\Entity\Payment newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Payment[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Payment|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Payment saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Payment patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Payment[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Payment findOrCreate($search, callable $callback = null, $options = [])
 */
class PaymentsTable extends AppTable
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('payments');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->integer('id')
            ->allowEmptyString('id', null, 'create');

        $validator
            ->notEmptyString('payment_type');

        $validator
            ->scalar('name')
            ->maxLength('name', 100)
            ->requirePresence('name', 'create')
            ->notEmptyString('name');

        $validator
            ->scalar('credentials')
            ->requirePresence('credentials', 'create')
            ->notEmptyString('credentials');

        $validator
            ->dateTime('created_at')
            ->requirePresence('created_at', 'create')
            ->notEmptyDateTime('created_at');

        $validator
            ->dateTime('updated_at')
            ->allowEmptyDateTime('updated_at');

        return $validator;
    }

    public function addPaymentSetup($data)
    {
        $result = false;
        $data = array_map('trim', $data);
        $payment = $this->newEntity();
        //$payment = $this->patchEntity($payment, $data);
        $payment->member_id = $data['member_id'];
        $payment->payment_type = $data['payment_type']; // 1 = Authorized.net
        $payment->credentials = $data['credentials'];
        $payment->name = $data['name'];
        $payment->created_at = $this->getSystemCurrentTimeStamp();
//        $memberExport->updated_at = $this->getSystemCurrentTimeStamp();
        if ($savedData = $this->save($payment)) {
            $result = $savedData;
        };
        return $result;
    }

    public function getPaymentSetupsByMemberId($member_id,$searchString = ""){
        $result = [];
        if($member_id > 0){
           $pamentsetups = $this->find()->where(['member_id'=>$member_id,$searchString]);
           if(!empty($pamentsetups->toArray())){
              $result =  $pamentsetups;
           }
        }

        return $result;
    }

    public function getPaymentSetupById($member_id,$id){
        $result = [];
        if($id > 0 && $member_id > 0){
            $paymentsetup = $this->find()->where(['member_id'=>$member_id,'id'=>$id])->first();
            if(!empty($paymentsetup)){
                $result = $paymentsetup;
            }
        }
        return $result;
    }

    public function getPaymentSetupByMemberIdAndType($member_id,$payment_type){
        $result = [];
        if($payment_type > 0 && $member_id > 0){
            $paymentsetup = $this->find()->where(['member_id'=>$member_id,'payment_type'=>$payment_type])->first();
            if(!empty($paymentsetup)){
                if(!empty($paymentsetup->credentials)){
                    $paymentsetup->credentials = json_decode($paymentsetup->credentials,true);
                }
                $result = $paymentsetup;
            }
        }
        return $result;
    }

    public function updatePaymentSetupById($inputData){
        $result = [];
        if($inputData['id'] > 0){
            $paymentEntity = $this->find()->where(['id'=>$inputData['id']])->first();
            $paymentEntity->name = $inputData['name'];
            $paymentEntity->credentials = $inputData['credentials'];
            $paymentEntity->updated_at = $this->getSystemCurrentTimeStamp();
            if($this->save($paymentEntity)){
                $result = $paymentEntity;
            }
        }
        return $result;
    }
    
    
    public function getPaymentInfoByMemberIdPaymentType($member_id, $payment_type){
        
        $result = [];
        
        if($member_id > 0 && $payment_type > 0){
            $payment = $this->find()->where(['member_id'=>$member_id,'payment_type'=>$payment_type])->first();
            if(!empty($payment)){
                $result = $payment;
            }
        }
        
        return $result;
    }
}
