<?php
namespace App\Transformer\User;

use Vinkla\Hashids\Facades\Hashids;

class UserTransformer
{
    public function newBuildUserShelfBooks($data): array
    {
        return [
            'list' => $this->newEachUserShelfBooks($data)
        ];
    }

    private function newEachUserShelfBooks($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            $result[] = [
                'bid'               => Hashids::encode(getProp($item, 'id')),
                'book_name'         => getProp($item, 'name'),
                'cover'             => addPrefix(getProp($item, 'cover')),
                'read_time'         => transDate(getProp($item, 'read_time')),
            ];
        }

        return $result;
    }

    public function newBuildEarnings($data): array
    {
        return [
            'earnings' => $data['earnings'],
            'earning_records' => $this->newEachEarningRecords($data['earning_records']),
            'invite_records' => $this->newEachInviteRecords($data['invite_records']),
        ];
    }

    private function newEachEarningRecords($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            switch ($item['change_type']) {
                case '1':
                    $info = $item['nickname'].'　充值'.$item['charge_price'].'元';
                    $price = '+'.$item['change_amount'].'元';
                    $price_type = 1;
                    break;
                case '2':
                    $info = '提现　'.$item['change_amount'].'元';
                    $price = '-'.$item['change_amount'].'元';
                    $price_type = 0;
                    break;
                case '3':
                    $info = '提现失败　'.$item['change_amount'].'元';
                    $price = '+'.$item['change_amount'].'元';
                    $price_type = 1;
                    break;
                default:
                    $info = '';
                    $price = $item['change_amount'].'元';
                    $price_type = 1;
            }
            $result[] = [
                'info'              => $info,
                'change_amount'     => $price,
                'date'              => transDate(getProp($item, 'created_at'), 'Y-m-d'),
                'after_amount'      => getProp($item, 'after_amount').'元',
                'price_type'        => $price_type
            ];
        }

        return $result;
    }

    private function newEachInviteRecords($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            $result[] = [
                'nickname'  => getProp($item, 'nickname'),
                'date'      => transDate(getProp($item, 'bind_time'), 'Y-m-d')
            ];
        }

        return $result;
    }

    // 当前阅读
    public function newBuildCurrentBook($data): array
    {
        if (empty($data)) {
            return [];
        }

        $data = (array)$data;
        $data['bid'] = Hashids::encode($data['bid']);
        $data['cover'] = addPrefix($data['cover']);

        return $data;
    }
}
