<?php

namespace App\Controllers;

//use CodeIgniter\API\ResponseTrait;

use CodeIgniter\API\ResponseTrait;

class WrenchBlog extends BaseController
{
    use ResponseTrait;

    public function mobileBlogData()
    {
        $endpoint = "WRENCH_MOBILE_BLOG_DATA";
        $res1 = $this->getCache($endpoint);
        if (count($res1)==0){
            $rawData = $this->apiData(0);
            $res1= $rawData['payload']; //[0]['payload'];
            $this->saveCache($endpoint,$res1,50000);
        }
        $in =[];
        return $this->respond( $this->summaryReturnData($in,$res1), 200);
    }



    public function website(){

        $blog_id = 0;
        //  echo "EXYTACT INPUT DATA HERE";
        $raw_json = file_get_contents('php://input');
        $in = json_decode($raw_json, true);
       // log_message('critical', "Into BlogData-> ".serialize( $in['blog_id'])  );
        log_message('critical', "BLOG DATA CALL ********* ".serialize($in) );

        if (isset($in['blog_id'])){
            log_message('critical', "BlogData-> ".$in['blog_id'] );
            $blog_id = (int) $in['blog_id'];
        }

        if ($blog_id >  0 ){
            $endpoint = "WRENCH_ONE_BLOG_DATA-".$blog_id;
            $res1 = $this->getCache($endpoint);
            if (count($res1)==0){
                $rawData = $this->apiData($blog_id);
                $res1= $rawData['payload']; //[0]['payload'];
                $this->saveCache($endpoint,$res1,18000);
            }
        }
        else{
            $endpoint = "WRENCH_BLOG_DATA";
            $res1 = $this->getCache($endpoint);
            if (count($res1)==0){
                $rawData = $this->apiData($blog_id);
                $res1= $rawData['payload']; //[0]['payload'];
                $this->saveCache($endpoint,$res1);
            }
        }


        $limit = isset($in['limit']) ? $in['limit']: 0;
       if ($limit> 0){
           $res1['blogdata'] = array_slice($res1['blogdata'], 0, $limit);
       }


        $res1['blogconfig'] = [
            "media_url" => "https://blog.wrenchboard.com/wp-content/uploads",
            "site_url" => "https://blog.wrenchboard.com",
            "other_cofig2" => "NONE",
        ];
        return  $this->response->setJson($res1);
    }

    public function blogLimitedData($inData){
        log_message('critical', "blogLimitedData-> ".$inData );
        $res1 = $this->blogData('blogdata',$raw_array=['limit'=>$inData ]);

        $res1['blogdata'] = array_slice($res1['blogdata'], 0, $inData);

        return $this->response->setJson($res1);
    }

    public function index()
    {

    }


}

//use ResponseTrait;

//  protected $db;
//    public $con_name = 'wrench_dockerblog'; // 'wrench_blog';

//    private function apiDataDELETED($blog_id) {
//        $this->db = \Config\Database::connect($this->con_name);
//        $data = array();
//        $extra_filter  = "";
//        if ($blog_id > 0 ){
//            $extra_filter  = " AND p1.id = $blog_id ";
//        }
//
//        try {
//            $mysql = "SELECT  id, post_title, post_content,post_date,comment_count FROM wp_posts WHERE post_type='post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 9";
//            $mysql = "SELECT p1.id AS id,  p1.*, wm2.meta_value FROM  wp_posts p1 LEFT JOIN   wp_postmeta wm1
//            ON (wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = '_thumbnail_id' )
//            LEFT JOIN
//            wp_postmeta wm2
//            ON (wm1.meta_value = wm2.post_id AND wm2.meta_key = '_wp_attached_file' AND wm2.meta_value IS NOT NULL   )
//            WHERE
//            p1.post_status='publish'
//            AND p1.post_type='post' $extra_filter
//            ORDER BY p1.post_date DESC LIMIT 2000";
//
//            $query = $this->db->query($mysql);
//
//            $data['payload']['blogdata'] = $query->getResult('array');
//            $totalCount = count( $data['payload']['blogdata'] );
//
//            $randomIndex =  rand(1, $totalCount);
//
//            $data['payload']['featured']  =  $data['payload']['blogdata'][$randomIndex ];
//            $data['payload']['image_url'] = 'https://blog.wrenchboard.com/wp-content/uploads/';
//            $data['payload']['blog_url']  = 'https://blog.wrenchboard.com/';
//            $data['payload']['total'] = $totalCount;
//        } catch (Exception $ex) {
//
//        }
//
//        return $data;
//    }
