<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/14
 * Time: 下午3:32
 */

require_once(BASE_DATA_PATH . '/api/audio/getid3.php');


class block_filter
{
    protected $mSpecialId;
    protected $mItems;
    protected $mType;
    protected $mImagePath;

    public function __construct($block_type,$special_id,$items)
    {
        $this->mItems = $items;
        $this->mType = $block_type;
        $this->mSpecialId = $special_id;
    }

    public function filter()
    {
        if($this->mItems == false) {
            return false;
        }

        if($this->mType == 'adv_list' || $this->mType == 'home3' || $this->mType == 'home5' || $this->mType == 'home6' || $this->mType == 'home7' || $this->mType == 'goods') {
            return $this->normal();
        }
        elseif ($this->mType == 'home1') {
            return $this->home1();
        }
        elseif($this->mType == 'home2' || $this->mType == 'home4') {
            return $this->home2();
        } else {
            return false;
        }
    }

    protected function home1()
    {
        $image = $this->mItems['image'];
        if($this->to_local($image) == true) {
            $this->mItems['image'] = $image;
        }

        $show_type = $this->mItems['show_type'];
        $show_data = $this->mItems['show_data'];
        $this->calc_show($this->mItems['type'],$this->mItems['data'],$this->mItems['image'],$show_type,$show_data);
        $this->mItems['show_type'] = $show_type;
        $this->mItems['show_data'] = $show_data;
        $this->mItems['reserved'] = $this->reserved($show_type,$this->mItems['reserved'],$show_data);

        return $this->mItems;
    }
    protected function reserved($show_type,$reserved,$show_data)
    {
        if($show_type == 'goods_top')
        {
            if(empty($reserved)) return "";

            $regxp = '/([^=#]+=[^#]*)[#]?/i';
            $val = preg_match_all($regxp,$reserved,$match);
            if($val == false) return $reserved;

            $items = [];
            if(count($match) == 2)
            {
                foreach($match[1] as $val)
                {
                    $kv = preg_split('/=/',$val);
                    if(!empty($kv))
                    {
                        $k = trim($kv[0]);
                        $v = trim($kv[1]);
                        if(!empty($k)) {
                            $items[$k] = $v;
                        }
                    }
                }
            }
            if(array_key_exists('top',$items))
            {
                $top = intval($items['top']);
                if($top > 0)
                {
                    $result = [];
                    foreach ($items as $key => $val)
                    {
                        if($key == 'top') {
                            $url = urlencode($this->topimages(intval($val)));
                            $data = "image={$url}";
                        } else {
                            $val = urlencode($val);
                            $data = "{$key}={$val}";
                        }
                        $result[] = $data;
                    }
                    return implode("#",$result);
                }
                else {
                    return $reserved;
                }
            }
            else {
                return $reserved;
            }
        }
        elseif($show_type == 'audio')
        {
            $path = $this->audio_local($show_data);
            if($path != false)
            {
                $getID3 = new getID3();
                $info = $getID3->analyze($path);
                if(array_key_exists('playtime_seconds',$info))
                {
                    $sec = intval($info['playtime_seconds']);
                    return "playtime={$sec}";
                }
                else {
                    return $reserved;
                }
            } else {
                return $reserved;
            }
        }
        else {
            if(empty($reserved)) return "";
            return $reserved;
        }
    }

    protected function topimages($level)
    {
        global $config;
        $images = $config['hotlist_image'];
        if(array_key_exists($level,$images)) {
            $name = $images[$level];
            $url = RESOURCE_SITE_URL . "/mobile/defimg/{$name}";
            return $url;
        } else {
            return "";
        }
    }

    protected function home2()
    {
        if(true)
        {
            $show_type = $this->mItems['rectangle1_show_type'];
            $show_data = $this->mItems['rectangle1_show_data'];
            $this->calc_show($this->mItems['rectangle1_type'],$this->mItems['rectangle1_data'],$this->mItems['rectangle1_image'],$show_type,$show_data);
            $this->mItems['rectangle1_show_type'] = $show_type;
            $this->mItems['rectangle1_show_data'] = $show_data;
            $this->mItems['rectangle1_reserved'] = $this->reserved($show_type,$this->mItems['rectangle1_reserved'],$show_data);
        }

        if(true)
        {
            $show_type = $this->mItems['rectangle2_show_type'];
            $show_data = $this->mItems['rectangle2_show_data'];
            $this->calc_show($this->mItems['rectangle2_type'],$this->mItems['rectangle2_data'],$this->mItems['rectangle2_image'],$show_type,$show_data);
            $this->mItems['rectangle2_show_type'] = $show_type;
            $this->mItems['rectangle2_show_data'] = $show_data;
            $this->mItems['rectangle2_reserved'] = $this->reserved($show_type,$this->mItems['rectangle2_reserved'],$show_data);
        }
        if(true)
        {
            $show_type = $this->mItems['square_show_type'];
            $show_data = $this->mItems['square_show_data'];
            $this->calc_show($this->mItems['square_type'],$this->mItems['square_data'],$this->mItems['square_image'],$show_type,$show_data);
            $this->mItems['square_show_type'] = $show_type;
            $this->mItems['square_show_data'] = $show_data;
            $this->mItems['square_reserved'] = $this->reserved($show_type,$this->mItems['square_reserved'],$show_data);
        }

        return $this->mItems;
    }

    protected function calc_show($data_type,$data,$image,&$show_type,&$show_data)
    {
        if(empty($show_type))
        {
            if($data_type == 'goods') {
                $show_type = 'goods';
                $show_data = $data;
            }
            elseif($data_type == 'innerurl') {
                $show_type = 'webview';
                $show_data = $data;
            }
            elseif($data_type == 'video') {
                $show_type = 'video';
                $show_data = $data;
            }
            else {
                $show_type = 'image';
                $show_data = $image;
            }
        }
        else
        {
            if($show_type == 'image') {
                $show_data = $image;
            }
        }
    }

    protected function normal()
    {
        $items = [];
        foreach ($this->mItems['item'] as $image => &$item)
        {
            if($this->to_local($image) == true) {
                $item['image'] = $image;
            }

            $show_type = $item['show_type'];
            $show_data = $item['show_data'];
            $this->calc_show($item['type'],$item['data'],$item['image'],$show_type,$show_data);
            $item['show_type'] = $show_type;
            $item['show_data'] = $show_data;
            $item['reserved'] = $this->reserved($show_type,$item['reserved'],$show_data);

            $items[$image] = $item;
        }

        $title = $this->mItems['title'];
        $title = empty($title) ? "" : $title;
        return array('item' => $items,'title' => $title);
    }

    public function to_local(&$image)
    {
        if(util::start_with($image,BASE_SITE_URL))
        {
            $prefix = 's' . $this->mSpecialId;

            $dest_dir = BASE_ROOT_PATH.DS.DIR_UPLOAD.DS.ATTACH_MOBILE . DS . 'special' . DS . $prefix;
            if(file_exists($dest_dir) == false) {
                mkdir($dest_dir);
            }
            $length = strlen(BASE_SITE_URL);
            $src = BASE_ROOT_PATH . substr($image, $length, strlen($image) - $length);

            $info = pathinfo($src);
            $dest_name = $prefix . '_' . md5_file($src) . '.' .$info['extension'];
            copy($src,$dest_dir . DS . $dest_name);
            $image = $dest_name;

            return true;
        } else {
            return false;
        }
    }

    public function audio_local($file)
    {
        $path = BASE_DATA_PATH . "/upload/audio/{$file}";
        if(file_exists($path)) {
            return $path;
        } else {
            return false;
        }
    }
}