<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/4/7
 * Time: 下午5:04
 */

namespace user_session;

require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
require_once (BASE_ROOT_PATH . '/helper/session_helper.php');
require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
require_once (BASE_ROOT_PATH . '/helper/fcode/mfcode.php');


use fcode\mfcode;
use session_helper;
use commonid_helper;
use Log;

class fcode
{
    private $mCodes;
    private $mDirty;
    private $mFClear;

    public function __construct($clear = false)
    {
        $this->mCodes  = [];
        $this->mDirty  = false;
        $this->mFClear = $clear;

        if(isset($_SESSION['fcodes'])) {
            $this->mCodes = $_SESSION['fcodes'];
        }
        else {
            $this->onStatus();
        }
    }

    public function __destruct()
    {
        if($this->mFClear)
        {
            Log::record("fcode __destruct clear all fcodes.",Log::DEBUG);
            if(isset($_SESSION['fcodes'])) {
                unset($_SESSION['fcodes']);
            }

            $this->mDirty = false;
            return;
        }

        if($this->mDirty) {
            $_SESSION['fcodes'] = $this->mCodes;
        }
        $this->mDirty = false;
    }

    public function onStatus()
    {
        $this->mCodes = [];
        $this->mDirty = true;
        if(session_helper::logined() == false || empty(session_helper::cur_mobile())) {
            return;
        }

        $mod_fcode = Model('goods_fcode');
        $fcodes = $mod_fcode->getFcodeList(['mobile' => session_helper::cur_mobile()]);

        foreach ($fcodes as $item)
        {
            $fcoder = new mfcode($item);
            if($fcoder->un_used()) {
                $this->mCodes[$fcoder->commonid()][] = $item;
            }
        }
    }

    public function goods_has_code($goods_id,&$lock_num)
    {
        $lock_num = 0;
        $common_id = commonid_helper::instance()->common_id($goods_id);
        if($common_id == false) {
            return false;
        }

        return $this->usable_num($common_id,$lock_num);
    }

    public function usable_num($common_id,&$lock_num)
    {
        $lock_num = 0;
        if(session_helper::logined() == false) {
            return false;
        }

        if(array_key_exists($common_id,$this->mCodes))
        {
            $total = 0;
            $fcodes = $this->mCodes[$common_id];
            foreach ($fcodes as $item)
            {
                $fcoder = new mfcode($item);
                if($fcoder->can_use()) {
                    $total += 1;
                }
                if($fcoder->locked()) {
                    $lock_num += 1;
                }
            }
            return ($total == 0 ? false : $total);
        }
        else
        {
            return false;
        }
    }

    public function fetch($commonid,$num)
    {
        if($num <= 0) return false;

        if(array_key_exists($commonid,$this->mCodes))
        {
            $result = [];
            $fcodes = $this->mCodes[$commonid];
            foreach ($fcodes as $item)
            {
                $fcoder = new mfcode($item);
                if($fcoder->can_use())
                {
                    $result[] = $fcoder->fc_id();
                    if(--$num == 0) {
                        $this->mFClear = true;
                        return $result;
                    }
                }
            }
            return false;
        }
        else
        {
            return false;
        }
    }
}