<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/5/9
 * Time: 下午10:35
 */

namespace fcode;

require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
require_once (BASE_ROOT_PATH . '/helper/special_helper.php');


use StatesHelper;
use Log;
use special_manager;
use commonid_helper;
use algorithm;

class present_manager
{
    public static $stInstance;

    private $mFcodes;
    private $mCidBcodes;

    private function __construct()
    {
    }

    static public function instance()
    {
        if(self::$stInstance == null) {
            self::$stInstance = new present_manager();
        }

        if(StatesHelper::fetch_state('present_fcode')) {
            Log::record("present_manager reinit data.",Log::DEBUG);
            self::$stInstance->init();
        }

        return self::$stInstance;
    }

    private function init()
    {
        global $config;
        $special_id = $config['autosend_fcodes']['order_present'];
        if($special_id <= 0) return false;

        $blocks = special_manager::instance()->special($special_id,$goods_ids);

        if(empty($blocks)) return false;

        $fcodes = [];
        $cid_bcodes = [];
        foreach ($blocks as $block)
        {
            $item_type  = $block['item_type'];
            if($item_type == 'home1')
            {
                $items = $block['items'];
                if(empty($items)) continue;

                $item = $items[0];
                $batch_code = $item['title'];
                if(empty($batch_code)) continue;
                $goods_id = intval($item['data']);
                $cid = commonid_helper::instance()->common_id($goods_id);
                if($cid > 0) {
                    $fcodes[$cid] = $batch_code;
                    $cid_bcodes[] = "{$cid}.{$batch_code}";
                }
            }
        }
        $this->mFcodes = $fcodes;
        sort($cid_bcodes);
        $this->mCidBcodes = $cid_bcodes;

        return true;
    }

    public function fetch($mobile,$session_id)
    {
        if(empty($mobile) || $this->can_send($mobile) == false)
            return false;

        foreach ($this->mFcodes as $commonid => $batch_code)
        {
            $oper = new operator($commonid,$batch_code,$mobile,$session_id);

            if($oper->grabed() == false)
            {
                $fcode = $oper->grab();
                if($fcode != false) {
                    return ['fcode' => $fcode,'common_id' => $commonid,'batch_code' => $batch_code];
                }
            }
        }
        return false;
    }

    private function can_send($mobile)
    {
        $mod_fcode = Model('goods_fcode');
        $codes = $mod_fcode->getFcodeList(['mobile' => $mobile]);

        $user_fcodes = [];
        foreach ($codes as $item)
        {
            $fcoder = new mfcode($item);
            $cid   = $fcoder->commonid();
            $bcode = $fcoder->batch_code();
            $user_fcodes[] = "{$cid}.{$bcode}";
        }
        sort($user_fcodes);
        $out = algorithm::set_intersection($user_fcodes,$this->mCidBcodes);
        return empty($out) ? true : false;
    }
}