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

namespace ugc;

use Exception;
use Log;
use errcode;

class special_object
{
    private $special_id;
    private $share_title;
    private $share_image;
    private $mb_special;
    private $contents;

    public function __construct()
    {
        $this->mb_special = Model('mb_special');
        $this->contents = [];
    }

    public function set_share($special_id,$title,$image) {
        $this->special_id  = $special_id;
        $this->share_title = empty($title) ? '' : $title;
        $this->share_image = empty($image) ? '' : $image;
    }
    public function add_item(UGContent $item)
    {
        $this->contents[] = $item;
    }
    public function count() {
        return count($this->contents);
    }

    public function save($user_id,$origin_content)
    {
        $sp_id = $this->save_special($user_id,$origin_content);
        if($sp_id == false) return false;
        $this->del_items($sp_id);

        $questions = [];
        $vote = [];
        $vote_result = [];

        $item_sort = 0;
        $qindex = 0;
        $desc = '';
        $fDesc = false;
        foreach ($this->contents as $item)
        {
            $data  = $item->format_block();
            if($data == false) {
                continue;
            }

            $data['special_id'] = $sp_id;
            $data['item_sort'] = $item_sort;
            $item_id = $this->save_item($data);
            $item_sort++;

            if($item_id == false) continue;

            $cur_type = $item->type();
            if($cur_type == 'text' && $fDesc == false)
            {
                $fDesc = true;
                $text = $item->text();
                $desc = mb_substr($text,0,128);
                if(!empty($desc)) {
                    $desc .= '>>>';
                }
            }
            elseif($cur_type == 'vote')
            {
                $vote_result[] = $item->vote_result();
                $vote[] = $item->vote_param();
            }
            elseif ($cur_type == 'question')
            {
                $question['question'] = $item->question();
                $question['answer'] = $item->answer();
                $questions[$qindex] = $question;

                $qindex++;
            }
            else {

            }
        }

        $params = ['vote' => $vote,'questions' => $questions];
        $this->mb_special->editUserSpecial($this->special_id,['member_id' => $user_id,'special_id' => $sp_id],['params' => serialize($params),
            'vote_result' => serialize($vote_result),'description' => $desc]);
        return $sp_id;
    }

    private function del_items($special_id)
    {
        $this->mb_special->delMbSpecialItem(['special_id' => $special_id],$special_id);
    }

    private function save_special($user_id,$origin_content)
    {
        $param['share_title']  = $this->share_title;
        $param['special_desc'] = $this->share_title;
        $param['share_image']  = $this->share_image;
        $param['member_id']    = $user_id;
        $param['origin_data']  = serialize($origin_content);
        $param['editime']      = time();
        $param['state']        = 1;
        $param['from_user']    = 1;

        if($this->special_id > 0)
        {
            $result = $this->mb_special->editUserSpecial($this->special_id,['member_id' => $user_id,'special_id' => $this->special_id],$param);
            if($result == true) {
                return $this->special_id;
            } else {
                return false;
            }
        }
        else
        {
            $param['addtime'] = time();
            $result = $this->mb_special->addMbSpecial($param);
            return $result;
        }
    }

    private function save_item($data)
    {
        $result = $this->mb_special->addUserSpecialItem($data);
        if($result == false) return false;
        return $result['item_id'];
    }

    public function insert_divider($special_id,$item_sort)
    {
        $param['special_id'] = $special_id;
        $param['item_sort'] = $item_sort;
        $param['item_type'] = 'divider';
        $param['bg_type'] = content_config::def_item_bg_type;
        $param['bg_data'] = content_config::def_item_bg_color;
        $param['has_margin'] = 0;
        $param['bg_image'] = '';

        return $this->save_item($param);
    }
}

class generator
{
    private $mVotePos;
    private $mQuestionPos;

    public function __construct()
    {
        $this->mVotePos = 0;
        $this->mQuestionPos = 0;
    }

    public function crate_special($data,&$err)
    {
        if(empty($data)) {
            $err = ['code' => errcode::ErrParamter,'msg' => "参数不能为空"];
            return false;
        }

        $spobj = new special_object();
        $share_title = $data['share_title'];
        $share_image = $data['share_image'];
        $sp_id       = $data['special_id'];

        $spobj->set_share($sp_id,$share_title,$share_image);

        $items = $data['items'];

        $index = 0;
        foreach ($items as $item)
        {
            $index++;
            try
            {
                $type = $item['type'];

                if($type == 'images') {
                    $obj_item = new images_item($item);
                }
                elseif($type == 'text') {
                    $obj_item = new text_item($item);
                }
                elseif($type == 'video') {
                    $obj_item = new video_item($item);
                }
                elseif ($type == 'goods') {
                    $obj_item = new goods_item($item);
                }
                elseif($type == 'vote') {
                    $obj_item = new vote_item($item,$this->mVotePos);
                    $this->mVotePos++;
                }
                elseif($type == 'question') {
                    $obj_item = new question_item($item);
                    $this->mQuestionPos++;
                }
                else {
                    continue;
                }
                $spobj->add_item($obj_item);
            }
            catch (Exception $ex) {
                $msg = "第{$index}项，{$ex->getMessage()}";
                $err = ['code' => errcode::ErrUGC,'msg' => $msg];
            }
        }
        if($spobj->count() > 0) {
            return $spobj;
        } else {
            return false;
        }
    }
}