<?php

namespace backend\models;

use Yii;
use yii\web\UploadedFile;


/**
 * This is the model class for table "book".
 *
 * @property integer $id
 * @property string $title
 * @property string $author
 * @property string $publisher
 * @property string $image
 * @property string $description
 * @property integer $categoryId
 * @property integer $type
 * @property string $attachment
 * @property integer $view
 * @property BookMark[] $bookMarks
 * @property Chapter[] $chapters
 */
class Book extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */


    public $file;
    public $attach;
    public static function tableName()
    {
        return 'book';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            /*[['avatar', 'filename', 'image'], 'safe'],
            [['image'], 'file', 'extensions'=>'jpg, gif, png'],*/
//            [['attach'], 'file', 'skipOnEmpty' => false, 'extensions' => 'epub, pdf'],
//            [['attach'], 'file','extensions' => 'epub, pdf'],
            [['title','categoryId'], 'required'],
            [['id', 'type', 'categoryId','view'], 'integer'],
            [['title', 'author', 'publisher'], 'string', 'max' => 255],
            [['file'], 'file','extensions' => 'jpg,png,gif,jpeg'],
            [['attach'], 'file','extensions' => 'epub,pdf'],
            //[['file','attach'], 'file'],
            [['description'], 'safe'],
//            ['attach', 'validateAttachment'],

            /*[['file'], 'file', 'maxFiles' => 10],*/
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'author' => 'Author',
            'publisher' => 'Publisher',
            'image' => 'Image',
            'description'=>'Description',
            'categoryId'=> 'Category',
            'type' => 'Type',
            'attachment' => 'Attachment',
            'view' => 'View',
            'file' => 'File format: jpg,png,gif,jpeg',
            'attach' => 'File format: epub,pdf'
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBookMarks()
    {
        return $this->hasMany(BookMark::className(), ['bookId' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getChapters()
    {
        return $this->hasMany(Chapter::className(), ['bookId' => 'id']);
    }

    public function getImageUrl()
    {
       // echo Yii::$app->basePath . '/images/' ;exit;
        Yii::$app->params['uploadUrl'] = Yii::$app->urlManager->baseUrl . '/images/book/';
        $avatar = isset($this->image) ? $this->image : 'default_user.jpg';
        return Yii::$app->params['uploadUrl'] . $avatar;
    }

    public static function deleteImage($oldImage) {
        if(strlen($oldImage)>0){
            $image = Yii::$app->basePath . '/web/images/book/' . $oldImage;
            if(is_file($image)){
                unlink($image);
            }
        }
        return false;
    }

    public static function deleteAttachment($oldAttach) {
        if(strlen($oldAttach)>0){
            $attachment = Yii::$app->basePath . '/uploads/attachment/' . $oldAttach;
            if(is_file($attachment)){
                unlink($attachment);
            }
        }
        return false;
    }

    public static function fileEpub($name_file)
    {
        $type = 'epub';
        if($type === $name_file)
            return true;

    }

    public static function filePdf($name_file)
    {
        $type = 'pdf';
        if($type === $name_file)
            return true;

    }

    public function validateAttachment($name_file)
    {
        $check = $this->fileEpub($name_file);
        $check2 = $this->filePdf($name_file);
        if($check == true || $check2 == true){
            Yii::$app->getSession()->setFlash('error', '<p style="color:red">'.'New Password same Current Password!'.'</p>');
        }

    }

}
