<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use common\components\AccountAwareBehavior;
use common\components\UserAwareBehavior;
use yii\base\Object;
use common\models\Transfer;

/**
 * This is the model class for table "money_movement".
 *
 * @property integer $id_money_movement
 * @property integer $id_account
 * @property integer $id_user
 * @property string $name
 * @property integer $type
 * @property integer $money
 * @property string $comment
 * @property string $created_at
 * @property string $updated_at
 */
class MoneyMovement extends \yii\db\ActiveRecord
{

	const TYPE_OUT = 10;
	const TYPE_IN  = 20;
	
	const STATUS_PAID = 20;
	const STATUS_STORNO = 30;
	
	public $_account;
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'money_movement';
    }

    
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
    	return ArrayHelper::merge(  [
    			[
    			'class' => TimestampBehavior::className(),
    			'value' => function(){ return date('Y-m-d H:i:s' , time()); }
    			],
    			[
    			'class' => AccountAwareBehavior::className(),
    			],
    			[
    			'class' => UserAwareBehavior::className(),
    			],
    	], parent::behaviors());
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_account',   'name',   'money'], 'required'],
            [['id_account' ,  'money'], 'integer' , 'min' => 0],
            [['name'], 'string', 'max' => 64],
            [['comment'], 'string', 'max' => 255],
            [['id_account'], 'validateAccount'],
        	[['type'] , 'in','range' => array_keys(MoneyMovement::types())],
        ];
    }

    public function validateAccount($attribute,$params){
    	$this->_account  = Account::findOne(['id_account' => $this->id_account]  );
    	if (  !isset( $this->_account )  ){
    		$this->addError($attribute, Yii::t('common/money-movement', 'Invalid account!'));
    	}
    	
    }
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_money_movement' => Yii::t('common/money-movement', 'Id Money Movement'),
            'id_account' => Yii::t('common/money-movement', 'Id Account'),
            'id_user' => Yii::t('common/money-movement', 'Id User'),
            'name' => Yii::t('common/money-movement', 'Name'),
            'type' => Yii::t('common/money-movement', 'Type'),
            'humanType' => Yii::t('common/money-movement', 'Type'),
            'money' => Yii::t('common/money-movement', 'Money'),
            'comment' => Yii::t('common/money-movement', 'Comment'),
            'created_at' => Yii::t('common/money-movement', 'Created At'),
            'updated_at' => Yii::t('common/money-movement', 'Updated At'),
        	'accountName' => Yii::t('frontend/money-movement', 'Account'),
        	'userName' => Yii::t('frontend/money-movement', 'User'),
        ];
    }
    
    
    public function afterSave($insert, $changedAttributes){
    	parent::afterSave($insert, $changedAttributes);
    	if ( $insert) {
    		$this->createTransfer();
    	}
    	
    }
    
    protected function createTransfer(){
    	$transfer = Transfer::createMoneyMovementOutTransfer($this->_account, $this);
    	$transfer->id_user = Yii::$app->user->id;
    	$transfer->save();
    }
    
    public static function types(){
    	return [
    			self::TYPE_IN => "Betét",
    			self::TYPE_OUT => "Kivét",
    			
    	];
    }
    
    public static function typeName($type){
    	$types = MoneyMovement::types();
    	$result = "";
    	if ( array_key_exists($type, $types)){
    		$result =  $types[$type];
    	}
    	return $result;
    }
    
    
    public function getHumanType(){
    	return self::typeName($this->type);
    }
    
    public static function statuses( ) {
    	return [
    			MoneyMovement::STATUS_PAID => 'Fizetve',
    			MoneyMovement::STATUS_STORNO => 'Törölve',
    	];
    }
    
    public static function toStatusName($id_status){
    	$result = "Ismeretlen";
    	$statuses = MoneyMovement::statuses();
    	if ( array_key_exists($id_status, $statuses)){
    		$result = $statuses[$id_status];
    	}
    	return $result;
    }
    
    public function getStatusName(){
    	return static::toStatusName($this->status);
    }
}
