<?php

namespace App\Http\Controllers\Goodnight;

use App\Http\Controllers\Miniprogram\Auth;
use App\Http\Resources\Goodnight\RankCollection;
use App\Models\Goodnight\ThumbModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RankController extends Controller
{
    //

    public function total(Request $request)
    {
        $page = $request->input('page', 1);
        $per_page = $request->input('per_page', 100);
        $builder = \Cache::get("goodnight:rank:total", collect());
        $total = $builder->count();
        $last_page = ceil($total / $per_page);
        $voices = $builder->slice(($page - 1) * $per_page, $per_page)->values();
        foreach ($voices as $voice) {
            try {
                $uid = Auth::auth();
                // 自己对这张卡片的操作
                $self = array(
                    'like' => false,
                    'is_self' => false
                );
                if ($voice->uid == $uid) {
                    $self['is_self'] = true;
                }
                if (
                    ThumbModel::where([
                    ['voice_id', $voice->id],
                    ['uid', $uid]
                    ])->exists()
                ) {
                    $self['like'] = true;
                }
                $voice->self = $self;
            } catch (\Exception $e) {
            }
        }
        return (new RankCollection($voices))
            ->additional([
                'meta' => [
                    'current_page' => $page,
                    'per_page' => $per_page,
                    'last_page' => $last_page,
                    'total' => $total,
                ]
            ]);
    }


    public function daily(Request $request)
    {
        $page = $request->input('page', 1);
        $per_page = $request->input('per_page', 100);
        $builder = \Cache::get("goodnight:rank:daily:" . date("Y-m-d"), collect());
        $total = $builder->count();
        $last_page = ceil($total / $per_page);
        $voices = $builder->slice(($page - 1) * $per_page, $per_page)->values();
        foreach ($voices as $voice) {
            try {
                $uid = Auth::auth();
                // 自己对这张卡片的操作
                $self = array(
                    'like' => false,
                    'is_self' => false
                );
                if ($voice->uid == $uid) {
                    $self['is_self'] = true;
                }
                if (
                    ThumbModel::where([
                    ['voice_id', $voice->id],
                    ['uid', $uid]
                    ])->exists()
                ) {
                    $self['like'] = true;
                }
                $voice->self = $self;
            } catch (\Exception $e) {
            }
        }
        return (new RankCollection($voices))
            ->additional([
                'meta' => [
                    'current_page' => $page,
                    'per_page' => $per_page,
                    'last_page' => $last_page,
                    'total' => $total,
                ]
            ]);
    }


    public function topic(Request $request)
    {
        $page = $request->input('page', 1);
        $per_page = $request->input('per_page', 100);
        $builder = \Cache::get("goodnight:rank:topic", collect());
        $total = $builder->count();
        $last_page = ceil($total / $per_page);
        $voices = $builder->slice(($page - 1) * $per_page, $per_page)->values();
        foreach ($voices as $voice) {
            try {
                $uid = Auth::auth();
                // 自己对这张卡片的操作
                $self = array(
                    'like' => false,
                    'is_self' => false
                );
                if ($voice->uid == $uid) {
                    $self['is_self'] = true;
                }
                if (
                    ThumbModel::where([
                    ['voice_id', $voice->id],
                    ['uid', $uid]
                    ])->exists()
                ) {
                    $self['like'] = true;
                }
                $voice->self = $self;
            } catch (\Exception $e) {
            }
        }
        return (new RankCollection($voices))
            ->additional([
                'meta' => [
                    'current_page' => $page,
                    'per_page' => $per_page,
                    'last_page' => $last_page,
                    'total' => $total,
                ]
            ]);
    }
}
