<?php
namespace App\Model\Table;

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

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

        $this->setTable('video_settings');
        $this->setDisplayField('id');
        $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('section_name')
            ->maxLength('section_name', 100)
            ->requirePresence('section_name', 'create');
            //->notEmptyString('section_name');

        $validator
            ->scalar('video_url')
            ->maxLength('video_url', 500);
           // ->allowEmptyString('video_url');

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

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

        return $validator;
    }
    
    
    public function getVideoSettingsList($status = 1){
        $result = [];
        $clause = [];
        if($status > 0){
            $clause['status'] = $status;
        }
        $vSettings = $this->find()->where($clause)->orderAsc('sequence')->toArray();
        if(!empty($vSettings)){
            foreach($vSettings as $setting){
                $result['section_name_list'][$setting->id] = $setting->section_name;
                $result['url_list'][$setting->id] = $setting->video_url;
                $result['url_sequence'][$setting->id] = $setting->sequence;
                $result['status_list'][$setting->id] = $setting->status;
                $result['actual_list'][$setting->id] = $setting->is_actual_section;
                $result['training_list'][$setting->id] = $setting->is_training_section;
                $result['url_list_by_section'][$setting->section_name] = $setting->video_url;
            }
        }
        
        return $result;
    }
    
    
    public function updateUrl($id,$inputdata){
        $result = false;
        if($id > 0){
            $conditions['id'] = $id;
            
            $fields['video_url'] = $inputdata['video_url'];
            if(!empty($inputdata['name'])){
                $fields['section_name'] = $inputdata['name'];
            }
            $fields['status'] = $inputdata['status'];
            $fields['is_actual_section'] = $inputdata['is_actual_section'];
            $fields['is_training_section'] = $inputdata['is_training_section'];
            $fields['sequence'] = $inputdata['sequence'];
            $fields['updated_at'] = $this->getSystemCurrentTimeStamp();

            if($this->updateAll($fields, $conditions)){
                $result = true;
            }
        }
        
        return $result;
    }

    public function deleteById($id){
        $result = false;
        if($id > 0){
            if($this->deleteAll(['id'=>$id])){
                $result = true;
            }
        }

        return $result;
    }


    public function add($inputData){
        $result = null;
        $current_date_time = $this->getSystemCurrentTimeStamp();
        
        $entity = $this->newEntity($inputData);
        $entity->created_at = $current_date_time;

        if($this->save($entity)){
            $result = $entity;
        }

        return $result;
    }


    public function saveOrdering($inputData){
        $result = false;
        $clause['status'] = 1;
        $settings_assoc = [];
        $vSettings = $this->find()->where($clause)->orderAsc('sequence')->toArray();

        if(!empty($vSettings)){
            foreach($vSettings as $setting){
                $settings_assoc[$setting->id]['id'] = $setting['id'];
                $settings_assoc[$setting->id]['section_name'] = $setting['section_name'];
                $settings_assoc[$setting->id]['video_url'] = $setting['video_url'];
                $settings_assoc[$setting->id]['sequence'] = $setting['sequence'];
                $settings_assoc[$setting->id]['status'] = $setting['status'];
                $settings_assoc[$setting->id]['created_at'] = $setting['created_at'];
                $settings_assoc[$setting->id]['updated_at'] = $setting['updated_at'];
            }
        }

        if(!empty($inputData)){
            $count = 0;
            foreach ($inputData as $id) {
                $count++;
                if($this->updateAll(['sequence'=>$count],['id'=>$id])){
                    
                }
            }  
        }
        
        return true;;
    }


    public function getActualVideoSettingsList($status = 1){
        $result = [];
        $clause = [];
        if($status > 0){
            $clause['status'] = $status;
        }
        $clause['is_actual_section'] = 1;
        $vSettings = $this->find()->where($clause)->orderAsc('sequence')->toArray();
        if(!empty($vSettings)){
            foreach($vSettings as $setting){
                $result['section_name_list'][$setting->id] = $setting->section_name;
                $result['url_list'][$setting->id] = $setting->video_url;
                $result['url_sequence'][$setting->id] = $setting->sequence;
                $result['status_list'][$setting->id] = $setting->status;
                $result['actual_list'][$setting->id] = $setting->is_actual_section;
                $result['training_list'][$setting->id] = $setting->is_training_section;
                $result['url_list_by_section'][$setting->section_name] = $setting->video_url;
            }
        }
        
        return $result;
    }


    public function getTrainingVideoSettingsList($status = 1){
        $result = [];
        $clause = [];
        if($status > 0){
            $clause['status'] = $status;
        }
        $clause['is_training_section'] = 1;
        $clause['and'] = ['video_url IS NOT NULL','video_url != ""'];
        $vSettings = $this->find()->where($clause)->orderAsc('sequence')->toArray();
        if(!empty($vSettings) ){
            foreach($vSettings as $setting){
                $result['section_name_list'][$setting->id] = $setting->section_name;
                $result['url_list'][$setting->id] = $setting->video_url;
                $result['url_sequence'][$setting->id] = $setting->sequence;
                $result['status_list'][$setting->id] = $setting->status;
                $result['actual_list'][$setting->id] = $setting->is_actual_section;
                $result['training_list'][$setting->id] = $setting->is_training_section;
                $result['url_list_by_section'][$setting->section_name] = $setting->video_url;
            }
        }
        return $result;
    }
}
