<?php

class ShopController extends Controller {

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
            array('allow',
                'actions' => array('index', 'info', 'buy', 'cat', 'dearshop', 'catdear'),
                'users' => array('@'),
            ),
            array('deny',
                'users' => array('*'),
            //'deniedCallback' => $this->redirect('?r=site/index');
            )
        );
    }

    public function actions() {
        return array(
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionInfo($id) {
        $model = Clothes::findClothes($id);
        $this->render('info', array(
            'model' => $model,
        ));
    }

    public function actionCat($id) {
        if ($id >= 1 AND $id <= 11) {
            $user = Users::findUser();

            $criteria = new CDbCriteria();
            $criteria->condition = '(side = :userSide OR side = 3) AND donate = :count AND type = :id';
            $criteria->params = array(':userSide' => Yii::app()->user->side, ':count' => '0', ':id' => $id);
            $count = Clothes::model()->count($criteria);
            $pages = new CPagination($count);
            $pages->pageSize = 6;
            $pages->applyLimit($criteria);
            $models = Clothes::model()->findAll($criteria);

            $this->render('cat', array(
                'models' => $models,
                'pages' => $pages,
                'level' => $user->level,
            ));
        } else
            $this->redirect(array('/shop/index'));
    }
    
    
    public function actionCatdear($id) {
        if ($id >= 1 AND $id <= 11) {
            $user = Users::findUser();

            $criteria = new CDbCriteria();
            $criteria->condition = '(side = :userSide OR side = 3) AND money = :count AND type = :id';
            $criteria->params = array(':userSide' => Yii::app()->user->side, ':count' => '0', ':id' => $id);
            $count = Clothes::model()->count($criteria);
            $pages = new CPagination($count);
            $pages->pageSize = 6;
            $pages->applyLimit($criteria);
            $models = Clothes::model()->findAll($criteria);
            $this->render('catdear', array(
                'models' => $models,
                'pages' => $pages,
                'level' => $user->level,
            ));
        } else
            $this->redirect(array('/shop/catdear'));
    }

    public function actionBuy($id) {
        $model = Clothes::findClothes($id);
        $user = Users::findUser();

        //запрет на покупку предметов для стороны противника
        if ($model->side != $user->side AND $model->side != 3) {
            Yii::app()->user->setFlash('error', 'Вы не можете купить данный предмет');
            $this->redirect(array('shop/cat/' . $model->type));
            exit();
        }

        if ($user->money >= $model->money AND $user->donate >= $model->donate) {
            $inventory = new Inventory();
            $inventory->item = $model->id;
            $inventory->user = Yii::app()->user->id;
            $inventory->title = $model->title;
            $inventory->health = $model->health;
            $inventory->attack = $model->attack;
            $inventory->deff = $model->deff;
            $inventory->accuracy = $model->accuracy;
            $inventory->dodge = $model->dodge;
            $inventory->level = $model->level;
            $inventory->side = $model->side;
            $inventory->type = $model->type;
            $inventory->improvement = $model->improvement;
            $inventory->money = $model->money;
            $inventory->donate = $model->donate;
            $inventory->other_value = $model->other_value;
            $inventory->crit = $model->crit;
            $inventory->acrit = $model->acrit;

            $inventory->save(false);

            $user->money -= $model->money;
            $user->donate -= $model->donate;
            $user->save(FALSE);
            Yii::app()->user->setFlash('success', 'Вы успешно купили "' . $model->title . '"');
        } else {
            Yii::app()->user->setFlash('error', 'У вас не хватает денег на покупку "' . $model->title . '"');
        }
        if (intval($_GET['fight'])==1) $this->redirect(array('arena/index'));
        if ($model->donate != 0) $this->redirect(array('shop/catdear', 'id' => $model->type));
        $this->redirect(array('shop/cat', 'id' => $model->type));
    }

    public function actionIndex() {

        $this->render('index', array(
        ));
    }

    public function actionDearshop() {

        $this->render('dearshop', array(
        ));
    }

}
