<?php

namespace App\Modules\UserTask\Services;

use App\Modules\User\Models\User;
use App\Modules\UserTask\Models\Task;
use Exception;
use Illuminate\Support\Facades\DB;

/**
 * @property Task $task
 * @property int $uid
 */
abstract class BaseTask
{
    /**
     * 未符合条件
     */
    const under_status = 0;
    /**
     * 符合条件
     */
    const up_status = 1;
    /**
     * 已领取
     */
    const get_status = 2;
    /**
     * 用户书币奖励
     */
    const coin_reward = 1;
    /**
     * 阅读卡奖励
     */
    const read_card_reward = 2;
    /**
     * 注册 
     */
    const register = 'register';
    /**
     * 加桌
     */
    const add_desk = 'add_desk';
    /**
     * 绑定手机号
     */
    const bind_phone = 'bind_phone';
    /**
     * 充值
     */
    const charge = 'charge';
    /**
     * 阅读
     */
    const read = 'read';

    /**
     * 新用户任务code组
     */
    const new_user_codes = [
        'user_register',
        'add_desk',
        'bind_phone',
    ];
    /**
     * 用户每日任务code组
     */
    const date_codes = [
        'date_charge_reward',
        'date_read_reward',
        'date_subscribe_reward',
    ];

    protected $task;
    protected $uid;

    /**
     * 检查任务
     */
    public abstract function checkUserTask();
    /**
     * 添加用户任务
     */
    public abstract function addUserTask();
    /**
     * 查找用户任务
     * @param int $status 任务状态
     */
    public abstract function findUserTask(int $status);

    public function __construct(int $uid, ?Task $task)
    {
        $this->task = $task ?? new Task;
        $this->uid = $uid;
    }

    /**
     * 设置任务
     */
    public function setTask(Task $task)
    {
        $this->task = $task;
    }

    /**
     * 处理添加用户任务
     */
    public function handleAddUserTask()
    {
        if (!$this->checkUserTask()) {
            $this->addUserTask();
        }
    }

    /**
     * 处理用户任务奖励
     * @return int
     */
    public function handleUserTaskReward()
    {
        $user_task = $this->findUserTask(self::up_status);
        if ($user_task) {
            try {
                DB::beginTransaction();
                $user_task->status = self::get_status;
                $user_task->save();
                switch ($this->task->reward_type) {
                    case self::coin_reward:
                        $this->coinReward();
                        break;
                    case self::read_card_reward:
                        $this->readCardReward();
                        break;
                }
                DB::commit();
                return 1;
            } catch (Exception $e) {
                myLog('user_task')->error($e->getMessage());
                DB::rollback();
                return -1;
            }
        } else {
            return 0;
        }
    }

    /**
     * 书币奖励
     */
    protected function coinReward()
    {
        User::where('id', $this->uid)->update(
            [
                'balance' => DB::raw('balance+' . $this->task->value),
                'reward_balance' => DB::raw('reward_balance+' . $this->task->value)
            ]
        );
    }

    /**
     * 阅读卡奖励
     */
    protected function readCardReward()
    {
    }
}
