<?php

namespace App\Http\Controllers\Admin\AuditMedia;

use App\Events\AuditMediaDeal;
use App\Http\Controllers\Controller;
use App\Models\Media\MediaSecure;
use App\Models\Media\MediaSecureImage;
use App\Models\Media\MediaSecureOther;
use App\Models\Media\MediaSecureText;
use App\Models\Media\MediaSecureVoice;
use Illuminate\Http\Request;

class AuditMediaController extends Controller
{
    public function listImage(Request $request)
    {
        $pages = array(
            'last_id' => $request->get('last_id', 1),
            'limit' => $request->get('limit', 20),
            'page' => $request->get('page', 1)
        );
        $field = $request->get('field');

        $build = MediaSecureImage::when($field, function ($query) use ($field) {
            return $query->ofField($field);
        })->when($request->filled('uid'), function ($query) use ($request) {
            return $query->where('uid', $request->uid);
        })->when($request->filled('result'), function ($query) use ($request) {
            return $query->where('result', $request->get('result'));
        })->when($request->filled('audit_suggestion'), function ($query) use ($request) {
            return $query->where('audit_suggestion', $request->get('audit_suggestion'));
        });

        $pages['total'] = $build->count();

        $list = $build->where('id', '>', $pages['last_id'])
            ->skip(($pages['page'] - 1) * $pages['limit'])
            ->take($pages['limit'])
            ->get();
        return array(
            'page' => $pages,
            'list' => $list
        );
    }

    public function listText(Request $request)
    {
        $pages = array(
            'last_id' => $request->get('last_id', 1),
            'limit' => $request->get('limit', 20),
            'page' => $request->get('page', 1)
        );
        $field = $request->get('field');

        $build = MediaSecureText::when($field, function ($query) use ($field) {
            return $query->ofField($field);
        })->when($request->filled('uid'), function ($query) use ($request) {
            return $query->where('uid', $request->uid);
        })->when($request->filled('result'), function ($query) use ($request) {
            return $query->where('result', $request->get('result'));
        })->when($request->filled('audit_suggestion'), function ($query) use ($request) {
            return $query->where('audit_suggestion', $request->get('audit_suggestion'));
        });
        $pages['total'] = $build->count();

        $list = $build->where('id', '>', $pages['last_id'])
            ->skip(($pages['page'] - 1) * $pages['limit'])
            ->take($pages['limit'])
            ->get();
        return array(
            'page' => $pages,
            'list' => $list
        );
    }

    public function listVoice(Request $request)
    {
        $pages = array(
            'last_id' => $request->get('last_id', 1),
            'limit' => $request->get('limit', 20),
            'page' => $request->get('page', 1)
        );
        $field = $request->get('field');

        $build = MediaSecureVoice::when($field, function ($query) use ($field) {
            return $query->ofField($field);
        })->when($request->filled('uid'), function ($query) use ($request) {
            return $query->where('uid', $request->uid);
        })->when($request->filled('result'), function ($query) use ($request) {
            return $query->where('result', $request->get('result'));
        })->when($request->filled('audit_suggestion'), function ($query) use ($request) {
            return $query->where('audit_suggestion', $request->get('audit_suggestion'));
        });

        $pages['total'] = $build->count();
        $list = $build->where('id', '>', $pages['last_id'])
            ->skip(($pages['page'] - 1) * $pages['limit'])
            ->take($pages['limit'])
            ->get();
        return array(
            'page' => $pages,
            'list' => $list
        );
    }

    public function audit(Request $request)
    {
        $type = $request->get('audit_type', 'text');
        $deals = $request->post();
        switch ($type) {
            case "text":
                $storage = new MediaSecureText();
                break;
            case "image":
                $storage = new MediaSecureImage();
                break;
            case "voice":
                $storage = new MediaSecureVoice();
                break;
            default:
                $storage = new MediaSecureOther();
                break;
        }
        foreach ($deals as $deal) {
            $media = $storage->find($deal['id']);
            $media->result = $deal['result'];
            $media->channel = MediaSecure::CHANNEL_MANUAL;
            $media->save();
            event(new AuditMediaDeal($media));
        }
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }
}
