<?php

namespace App\Model\Table;

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

/**
 * SoftwareVersions Model
 *
 * @method \App\Model\Entity\SoftwareVersion get($primaryKey, $options = [])
 * @method \App\Model\Entity\SoftwareVersion newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\SoftwareVersion[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\SoftwareVersion|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\SoftwareVersion saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\SoftwareVersion patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\SoftwareVersion[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\SoftwareVersion findOrCreate($search, callable $callback = null, $options = [])
 */
class SoftwareVersionsTable extends AppTable {

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config) {
        parent::initialize($config);

        $this->setTable('software_versions');
        $this->setDisplayField('title');
        $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
                ->scalar('title')
                ->maxLength('title', 200)
                ->requirePresence('title', 'create')
                ->notEmptyString('title');

        $validator
                ->scalar('descripton')
                ->allowEmptyString('descripton');

        $validator
                ->scalar('video_link')
                ->maxLength('video_link', 200)
                ->allowEmptyString('video_link');

        $validator
                ->notEmptyString('status');

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

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

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

        return $validator;
    }

    public function insertAgreementFormat($inputData) {
        $result = false;
        $softwareVersion = $this->newEntity();
        //$softwareVersion = $this->patchEntity($softwareVersion, $inputData);
        $softwareVersion->in_new = $inputData['in_new'];
        $softwareVersion->status = $inputData['status'];
        $softwareVersion->video_link = $inputData['video_link'];
        $softwareVersion->description = $inputData['description'];
        $softwareVersion->title = $inputData['title'];
        $softwareVersion->release_date = $inputData['release_date'];
        $softwareVersion->created_at = $this->getSystemCurrentTimeStamp();
        //pr($softwareVersion);exit;
        if ($this->save($softwareVersion)) {
            $result = true;
        }

        return $result;
    }

    public function updateSoftwareVersion($id,$inputData) {
        $result = false;
        if (!empty($id) && !empty($inputData)) {
            $conditions['id'] = $id;
            $inputData['updated_at'] = $this->getSystemCurrentTimeStamp();
            if ($this->updateAll($inputData, $conditions)) {
                $result = true;
            }
        }
        return $result;
    }

    public function getAllSoftwareVersions($search = [], $cols = []) {
        $result = "";
        if (isset($search['status']) && !empty($search['status'])) {
            $clause['status'] = $search['status'];
        }
        if (isset($search['search']) && !empty($search['search'])) {
            $clause['title Like'] = "%" . $search['search'] . "%";
        }


        $agreements = $this->find()->select($cols)->where($clause)->orderDesc('id');

        if (!empty(count($agreements) > 0)) {
            $result = $agreements;
        }


        return $result;
    }

    public function getSoftwareVersionById($id) {
        $result = [];
        if ($id > 0) {
            $clause['id'] = $id;
            $agreements = $this->find()->where($clause)->first();
            if (!empty($agreements)) {
                $result = $agreements;
            }
        }

        return $result;
    }

    public function deleteSoftwareVersionById($id) {
        $result = false;
        if ($id > 0) {
            $agreement = $this->get($id);
            if ($this->delete($agreement)) {
                $result = true;
            }
        }

        return $result;
    }

}
