<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "cinema".
 *
 * @property integer $id
 * @property string $codice
 * @property string $ctel
 * @property string $nome
 * @property string $visione
 * @property string $quartiere
 * @property string $indirizzo
 * @property string $regione
 * @property string $provincia
 * @property string $citta
 * @property string $telefono
 * @property integer $bar
 * @property string $serv_dis
 * @property string $url
 * @property string $pren_tel
 * @property string $latitudine
 * @property string $longitudine
 * @property string $visibile
 *
 * @property Sala[] $salas
 */
class Cinema extends \yii\db\ActiveRecord {

    private $_preferito;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'cinema';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['codice', 'ctel', 'nome'], 'required'],
            [['bar'], 'string', 'max' => 8],
            [['codice', 'ctel', 'visione', 'telefono', 'serv_dis', 'pren_tel', 'latitudine', 'longitudine'], 'string', 'max' => 32],
            [['nome', 'quartiere', 'regione', 'provincia'], 'string', 'max' => 128],
            [['indirizzo', 'citta', 'url'], 'string', 'max' => 256],
            [['preferito'], 'boolean'],
            [['preferito'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'codice' => 'Codice',
            'ctel' => 'Ctel',
            'nome' => 'Nome',
            'visione' => 'Visione',
            'quartiere' => 'Quartiere',
            'indirizzo' => 'Indirizzo',
            'regione' => 'Regione',
            'provincia' => 'Provincia',
            'citta' => 'Citta',
            'telefono' => 'Telefono',
            'bar' => 'Bar',
            'serv_dis' => 'Serv Dis',
            'url' => 'Url',
            'pren_tel' => 'Pren Tel',
        ];
    }

    public function getPreferito() {
        $pref = 0;

        if (!Yii::$app->user->isGuest) {

            $lastCinema = Voucher::find()->where(['utente' => Yii::$app->user->id, 'valido' => 1])->orderBy('data_creazione DESC')->one();
            if ($lastCinema != NULL && $lastCinema->cinema0->visibile == 0) {
                $lastCinema = NULL;
            }

            if ($lastCinema != NULL && $this->id == $lastCinema->cinema0->id) {
                $pref = 1;
            }
        }

        return $pref;
    }

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

    public static function searchForCity($citta) {
        // create ActiveQuery
        $query = self::find();

        if (!Yii::$app->user->isGuest)
            $query->select = ['*', "IF((SELECT cinema FROM voucher WHERE utente = " . Yii::$app->user->id . " ORDER BY data_creazione DESC LIMIT 0,1) = cinema.id, 1, 0) as preferito"];

        $query->where(['citta' => $citta, 'visibile' => 1]);

        $dataProvider = new \yii\data\ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 0
            ],
            'sort' => [
                'defaultOrder' => (!Yii::$app->user->isGuest)? ['preferito' => SORT_DESC] : NULL,
                'attributes' => [
                    'preferito' => [
                        'asc' => ['preferito' => SORT_ASC],
                        'desc' => ['preferito' => SORT_DESC],
                        'label' => 'preferito',
                        'default' => SORT_DESC
                    ],
                ],
            ]
        ]);

        // $dataProvider->setSort([
        //     'attributes' => [
        //         'id',
        //         'preferito' => [
        //             'asc' => ['preferito' => SORT_ASC],
        //             'desc' => ['preferito' => SORT_DESC],
        //             'label' => 'preferito',
        //             'default' => SORT_DESC
        //         ],
        //     ]
        // ]);

        return $dataProvider;
    }

    public static function searchForProvincia($provincia) {
        // create ActiveQuery
        $query = self::find();

        if (!Yii::$app->user->isGuest)
            $query->select = ['*', "IF((SELECT cinema FROM voucher WHERE utente = " . Yii::$app->user->id . " ORDER BY data_creazione DESC LIMIT 0,1) = cinema.id, 1, 0) as preferito"];

        $query->where(['provincia' => $provincia, 'visibile' => 1]);

        $dataProvider = new \yii\data\ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 0
            ],
            'sort' => [
                'defaultOrder' => (!Yii::$app->user->isGuest)? ['preferito' => SORT_DESC] : NULL,
                'attributes' => [
                    'preferito' => [
                        'asc' => ['preferito' => SORT_ASC],
                        'desc' => ['preferito' => SORT_DESC],
                        'label' => 'preferito',
                        'default' => SORT_DESC
                    ],
                ],
            ]
        ]);

        return $dataProvider;
    }

    public static function searchForRegione($regione) {
        // create ActiveQuery
        $query = self::find();

        if (!Yii::$app->user->isGuest)
            $query->select = ['*', "IF((SELECT cinema FROM voucher WHERE utente = " . Yii::$app->user->id . " ORDER BY data_creazione DESC LIMIT 0,1) = cinema.id, 1, 0) as preferito"];

        $query->where(['regione' => $regione, 'visibile' => 1]);

        $dataProvider = new \yii\data\ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 0
            ],
            'sort' => [
                'defaultOrder' => (!Yii::$app->user->isGuest)? ['preferito' => SORT_DESC] : NULL,
                'attributes' => [
                    'preferito' => [
                        'asc' => ['preferito' => SORT_ASC],
                        'desc' => ['preferito' => SORT_DESC],
                        'label' => 'preferito',
                        'default' => SORT_DESC
                    ],
                ],
            ]
        ]);

        return $dataProvider;
    }

    public function getCinemaUrl() {
        if (strstr($this->url, "http://") !== FALSE || strstr($this->url, "https://") !== FALSE) {
            return $this->url;
        } else {
            return "http://" . $this->url;
        }
    }

}
