<?php


namespace Heanup\App\Api\Controller\Novel;


use Heanup\App\Api\Controller;
use Heanup\Library\Book;

class Api extends Controller
{
    protected function main()
    {
        $do = $this->getRequest()->string("do", "index");
        $keyword = $this->getRequest()->string("keyword");
        $book_id = $this->getRequest()->intval("book_id");
        $chapter_id = $this->getRequest()->intval("chapter_id");
        $page = $this->getRequest()->intval("page", 1);
        $result = "[]";
        if ($do == "index") {
            $result = Book::getInstance()->getIndexData();
            $result = json_decode($result, true);
            $result = $result['data'];
        }
        if ($do == "search") {
            $result = Book::getInstance()->search($keyword);
            $result = json_decode($result, true);
            $response = [];
            foreach ($result['data'] as $item) {
                $response[] = [
                    'id' => $item['Id'],
                    'img' => $item['Img'],//Book::getInstance()->getImageUrl($item['Img']),
                    'name' => $item['Name'],
                    'author' => $item['Author'],
                    'introduce' => $item['Desc']
                ];
            }
            $result = $response;
        }
        if ($do == "detail") {
            $result = Book::getInstance()->getBookDetail($book_id);
            $result = json_decode(trim($result), true);
            $result = $result['data'];
            $response=[];
            $response['id']=$result['Id'];
            $response['name']=$result['Name'];
            $response['author']=$result['Author'];
            $response['sort']=$result['CName'];
            $response['state']=$result['BookStatus'];
            $response['time']=$result['LastTime'];
            $response['img']=Book::getInstance()->getImageUrl($result['Img']);
            $response['intro']=$result['Desc'];
            $response['news']=$result['LastChapter'];
            $chapter=Book::getInstance()->getChapter($book_id);
            $chapter=\GuzzleHttp\json_decode($chapter,true);
            $_chapter=[];
            foreach ($chapter['data']['list'] as $item) {
                foreach ($item['list'] as $val) {
                    $_chapter[]=$val;
                }
            }
            $response=[
                'data'=>$response,
                'list'=>$_chapter
            ];
            $result = $response;
        }
        if ($do == "list") {
            $chapter=Book::getInstance()->getChapter($book_id);
            $chapter=\GuzzleHttp\json_decode($chapter,true);
            $_chapter=[];
            foreach ($chapter['data']['list'] as $item) {
                foreach ($item['list'] as $key=>$val) {
                    $_chapter[]=[
                        'index'=>$key,
                        'id'=>$val['id'],
                        'name'=>$val['name']
                    ];
                }
            }
            $result = $_chapter;
        }
        if ($do == "content") {
            $result = Book::getInstance()->getContent($book_id, $chapter_id);
            $result = json_decode($result, true);
            $result = nl2br($result['data']['content']);
        }

        $this->showSuccess($result);
    }
}