<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "account_state".
 *
 * @property integer $id_account_state
 * @property integer $id_account
 * @property integer $type
 * @property integer $money
 * @property integer $banknote_5_ft
 * @property integer $banknote_10_ft
 * @property integer $banknote_20_ft
 * @property integer $banknote_50_ft
 * @property integer $banknote_100_ft
 * @property integer $banknote_200_ft
 * @property integer $banknote_500_ft
 * @property integer $banknote_1000_ft
 * @property integer $banknote_2000_ft
 * @property integer $banknote_5000_ft
 * @property integer $banknote_10000_ft
 * @property integer $banknote_20000_ft
 * @property integer $id_user
 * @property integer $collection_money
 * @property string $created_at
 * @property string $updated_at
 */
class AccountState extends \common\models\BaseFitnessActiveRecord
{
	
	const TYPE_OPEN = 10;
	const TYPE_CLOSE = 20;
	
	public $start_date;
	
	/**
	 * @inheritdoc
	 */
// 	public function behaviors()
// 	{
// 		return   [[
// 						'class' => TimestampBehavior::className(),
// 						'value' => function(){ return date('Y-m-d H:i' ); }
// 				] ];
// 	}
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'account_state';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
        	[['money' ], 'required'],
            [['type', 'money', 'banknote_5_ft', 'banknote_10_ft', 'banknote_20_ft', 'banknote_50_ft', 'banknote_100_ft', 'banknote_200_ft', 'banknote_500_ft', 'banknote_1000_ft', 'banknote_2000_ft', 'banknote_5000_ft', 'banknote_10000_ft', 'banknote_20000_ft' ], 'integer'],
        	[['comment' ], 'string' ,'max' => 255],
        	[['prev_state'  ], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_account_state' => Yii::t('common/account_state', 'Id Account State'),
            'id_account' => Yii::t('common/account_state', 'Id Account'),
            'accountName' => Yii::t('common/account_state', 'Id Account'),
            'type' => Yii::t('common/account_state', 'Type'),
            'typeName' => Yii::t('common/account_state', 'Type'),
            'money' => Yii::t('common/account_state', 'Money'),
            'banknote_5_ft' => Yii::t('common/account_state', 'Banknote 5 Ft'),
            'banknote_10_ft' => Yii::t('common/account_state', 'Banknote 10 Ft'),
            'banknote_20_ft' => Yii::t('common/account_state', 'Banknote 20 Ft'),
            'banknote_50_ft' => Yii::t('common/account_state', 'Banknote 50 Ft'),
            'banknote_100_ft' => Yii::t('common/account_state', 'Banknote 100 Ft'),
            'banknote_200_ft' => Yii::t('common/account_state', 'Banknote 200 Ft'),
            'banknote_500_ft' => Yii::t('common/account_state', 'Banknote 500 Ft'),
            'banknote_1000_ft' => Yii::t('common/account_state', 'Banknote 1000 Ft'),
            'banknote_2000_ft' => Yii::t('common/account_state', 'Banknote 2000 Ft'),
            'banknote_5000_ft' => Yii::t('common/account_state', 'Banknote 5000 Ft'),
            'banknote_10000_ft' => Yii::t('common/account_state', 'Banknote 10000 Ft'),
            'banknote_20000_ft' => Yii::t('common/account_state', 'Banknote 20000 Ft'),
            'id_user' => Yii::t('common/account_state', 'Id User'),
            'created_at' => Yii::t('common/account_state', 'Created At'),
            'updated_at' => Yii::t('common/account_state', 'Updated At'),
            'comment' => Yii::t('common/account_state', 'Comment'),
        ];
    }
    
    public function validatePrevState($attribute,$params){
    	
    	
    }
    
    public function isTypeOpen(){
    	return $this->type == AccountState::TYPE_OPEN;
    }
    public function isTypeClose(){
    	return $this->type == AccountState::TYPE_CLOSE;
    }
    
    public static function banknoteValues()
    {
        return [
            'banknote_5_ft' => 5,
            'banknote_10_ft' => 10,
            'banknote_20_ft' => 20,
            'banknote_50_ft' =>  50,
            'banknote_100_ft' => 100,
            'banknote_200_ft' => 200,
            'banknote_500_ft' => 500,
            'banknote_1000_ft' => 1000,
            'banknote_2000_ft' => 2000,
            'banknote_5000_ft' => 5000,
            'banknote_10000_ft' => 10000,
            'banknote_20000_ft' => 20000,
        ];
    }
    
    
    public function getAccount(){
    	return $this->hasOne( Account::className(), ["id_account" =>"id_account" ] ) ;
    }
    
    public function getAccountName(){
    	$result = "";
    	$account = $this->account;
    	if (isset($account)){
    		$result  = $account->name;
    	}
    	 
    	return $result;
    }
    
    public function getUser(){
    	return $this->hasOne( User::className(), ["id" =>"id_user" ] );
    }
    
    public function getPrevObject(){
    	return $this->hasOne( AccountState::className(), ["id_account_state" =>"prev_state" ] );
    }
    
    public function getUserName(){
    	$result = "";
    	$user = $this->user;
    	if (isset($this->user)){
    		$result = $user->username;
    	}
    	 
    	return $result;
    }
    public function getTypeName(){
    	$result = "";
    	$type =  AccountState::findType($this->type);
    	if (isset($type)){
    		$result = $type;
    	}
    	 
    	return $result;
    }
    
    public static function types(){
    	return [
    			self::TYPE_OPEN => Yii::t('common/account-state','Open'),
    			self::TYPE_CLOSE => Yii::t('common/account-state','Close'),
    	];
    }
    
    public static function findType($type){
    	$result = null;
    	$types = self::types();
    	if ( array_key_exists($type, $types)){
    		$result = $types[ $type ];
    	}
    	return $result; 
    }
    
    /**
     * Read last accountstates
     * @param $type int the type of accountstate to load
     * @param $user $id of user to load the account
     * @return common\models\AccountState
     * 
     * */
    public static function readLast($type, $user = null,$account = null){
    	$result = null;
    	$query = AccountState::find();
    	//filter type
    	if ( isset($type)){
    		$query->andWhere(['account_state.type' => $type]);
    	}
    	if ( isset( $account ) ){
    		$query->andWhere(["account_state.id_account" => $account ]);
    	}
    	
    	if ( isset( $user ) ){
    		$query->andWhere(["account_state.id_user" => $user ]);
    	}

    	$query->limit(1);
    	
    	$query->orderBy(['created_at' => SORT_DESC]);
    	$result = $query->one();
    	return $result;
    }
    
    function beforeSave($insert){
    	$result = parent::beforeSave($insert);
    	if ( $result ){
    		$start = null;
    		$end = null;
	    	$prev = null;
	    	$this->prev_money = 0;
	    	
	    	if ( $this->type == AccountState::TYPE_CLOSE){
	    		$lastOpen = AccountState::readLast(AccountState::TYPE_OPEN,\Yii::$app->user->id, $this->id_account);
	    		if ( $lastOpen != null ){
	    			$start = $lastOpen->created_at;
	    			$this->prev_state = $lastOpen->id_account_state;
	    			$this->prev_money = $lastOpen->money;
	    		}
	    		$end = date('Y-m-d H:i:s' );
// 	    		$end = Yii::$app->formatter->asDatetime(strtotime("now"), "php:Y-m-d H:i:s");
	    		$this->collection_money = Transfer::readPaid($start, $end, Yii::$app->user->id);
	    		
	    		
	    	}
	    	
    	}
    	return $result;
    	
    }
    
    public function hasDifferenceToPrevState(){
    	$result = false;
    	if (  $this->type == self::TYPE_CLOSE){
    		$result =  (  $this->prev_money + $this->collection_money) != $this->money;
    	}
    	return $result;
    }
    
    public function hasPlus(){
    	$result = false;
    	if (  $this->type == self::TYPE_CLOSE){
    		$result = (  $this->prev_money + $this->collection_money) < $this->money;
    	}
    	return $result;
    }
    
    public function hasMinus(){
    	$result = false;
    	if (  $this->type == self::TYPE_CLOSE){
    		$result =(  $this->prev_money + $this->collection_money) > $this->money;
    	}
    	return $result;
    }
    
    public function getSignedDiff(){
    		$result =  $this->money - (  $this->prev_money + $this->collection_money);
    	return $result;
    }
    
    public function getExpected(){
    	$result =    $this->prev_money + $this->collection_money ;
    	return $result;
    }
    
    
    public static function readLastForUser($type){
    	return static::readLast($type, Yii::$app->user->id);
    }
    
    
    public static function modelsToArray($models){
    	return ArrayHelper::toArray($models, [
    			'common\models\AccountState' => [
    					'id_account_state',
    					'id_account',
    					'money',
    			],
    	]);
    }
    
}
