<?php

namespace App\Http\Controllers;

use App\Fortuneteller;
use App\Http\Resources\FortunetellerResource;
use App\WpPosts;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class WpPostController extends Controller
{
    const ITEM_PER_PAGE = 10;

    public function index(Request $request)
    {
        $searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);
        $posts = WpPosts::with('meta')
            ->select('ID', 'post_title', 'post_date', 'guid')
            ->where('post_type', 'post')
            ->where('post_status', 'publish')
            ->where('ping_status', 'open')
            ->orderBy('post_date', 'DESC')
            ->paginate($limit);

        return response()->json($posts, 200);
    }

    public function all(Request $request)
    {
        /*->inRandomOrder()*/

        /*$searchParams = $request->all();
        $limit = Arr::get($searchParams, 'limit', static::ITEM_PER_PAGE);
        $posts = WpPosts::with('meta')
            ->select('ID', 'post_title', 'post_name', 'post_content', 'guid')
            ->where('post_type', 'post')
            ->where('post_status', 'publish')
            ->where('ping_status', 'open')
            ->orderBy('id')
            ->limit($limit)
            ->get();*/

        $rs = [];
        /*foreach ($posts as $item) {
            $item['post_content'] = $this->getShortContent($item['post_content']);
            $rs[] = $item;
        }*/

        return response()->json($rs, 200);
    }

    private function getShortContent($content = '') {
        $shortDesc = '';
        $content = str_replace('<!-- wp:paragraph -->', '<content>', $content);
        $content = str_replace('<!-- /wp:paragraph -->', '</content>', $content);
        $content = str_replace('\r\n', '', $content);
        $content = str_replace('\r', '', $content);
        $content = str_replace('\n', '', $content);
        preg_match('/<content>(.*?)<\/content>/s', $content, $matches);
        if (isset($matches[1])) $shortDesc = strip_tags($matches[1]);

        mb_regex_encoding('UTF-8');
        mb_internal_encoding("UTF-8");
        $list = $this->mb_str_split($shortDesc);
        $str = '';
        for ($i=0; $i <= 150; $i++) { 
            if (isset($list[$i])) $str .= $list[$i];
        }

        return $str;
    }

    private function mb_str_split($string) {
        return preg_split('/(?<!^)(?!$)/u', $string );
    }

    public function fortuneteller($code = 0) {
        $fortuneteller = Fortuneteller::notDeleted()->where('id', $code)->first();
        if (!isset($fortuneteller)) return response()->json(['errors' => 'Fortuneteller is not valid'], 403);

        return new FortunetellerResource($fortuneteller);
    }
}

/*function wpb_profile_shortcode($atts) {
    $message = '';
    $param = shortcode_atts(array('code' => '0'), $atts);
    if ($param['code'] != '') {
        $content = file_get_contents('https://uramori.jp/profile/api/wpposts/fortuneteller/' . $param['code']);
        $fortune = json_decode($content, true);
        $message .= '<table class="tbl_profile"><tbody>';
        foreach($fortune['data']['profile'] as $item) {
            $message .= '<tr>';
            $message .= '    <td>' . $item['title'] . '</td>';
            $message .= '    <td>' . $item['content'] . '</td>';
            $message .= '</tr>';
        }
        $message .= '</tbody></table>';
    }
    return $message;
}
add_shortcode('profile', 'wpb_profile_shortcode');*/