<?php

namespace App\Services\Complain;

use App\Exceptions\AlertException;
use App\Models\ComplaintModel;
use App\Models\User\UserModel;

abstract class ComplaintHandle
{

    public $complaint;
    public $be_user;
    public $fields = [];
    public $field;
    public $value = null;
    public $state;
    public $reply_state;
    public $reply_content;
    /**
     * 拉黑的类型
     * @var int $defriend_state
     */
    public $defriend_state;
    protected $check = [
        "photo_src" => "check_photo",
        "photo_1" => "photo_1_check",
        "photo_2" => "photo_2_check",
        "photo_3" => "photo_3_check",
        "photo_4" => "photo_4_check",
        "voice" => "voice_check",
    ];
    protected $user;
    protected $data;
    /**
     * 拉黑时间
     * @defriend_at int
     */
    protected $defriend_at;

    public function __construct(ComplaintModel $complaint)
    {
        $this->complaint = $complaint;
    }

    public static function make($id)
    {
        $complaint = ComplaintModel::findOrFail($id);
        switch ($complaint->type) {
            case "partner":
                return new PartnerComplaintHandle($complaint);
                break;
            case "contact":
                return new ContactComplaintHandle($complaint);
                break;
            case "pair":
                return new PairComplaintHandle($complaint);
                break;
            case "game":
                return new GameComplaintHandle($complaint);
                break;
            case "goodnight":
                return new GoodnightComplaintHandle($complaint);
                break;
            default:
                throw new \Exception("举报类型异常", 500);
        }
    }

    /**
     * 获取举报人
     */
    public function getUser()
    {
        $this->user = UserModel::findOrFail($this->complaint->uid);

        return $this->user;
    }

    /**
     * 获取被举报人
     */
    public function getBeUser()
    {
        $this->be_user = UserModel::findOrFail($this->complaint->be_uid);

        return $this->be_user;
    }

    public function setField($field)
    {
        array_push($this->fields, $field);
    }

    /**
     * 删除或修改字段
     * @return mixed
     */
    abstract public function erasing();

    /**
     * 举报
     * @return mixed
     */
    abstract public function reply();

    /**
     * 处理举报
     * @throws AlertException
     */
    public function updateState()
    {
        if ($this->complaint->reply_at > 0) {
            throw new AlertException("举报已经处理", 411);
        }
        $this->complaint->state = $this->state;
        $this->complaint->reply_content = $this->reply_content;
        $this->complaint->reply_at = time();
        $this->complaint->save();
    }

    /**
     * 设置拉黑时间
     * @param  int  $state
     */
    public function setDefriend(int $state)
    {
        switch ($state) {
            case "1":
                $this->defriend_at = time() + 7 * 24 * 3600;
                break;
            case "2":
                $this->defriend_at = time() + 30 * 24 * 3600;
                break;
            case "3":
                $this->defriend_at = 1893427200;
                break;
        }
        $this->defriend_state = $state;
    }


    public function getDefriendAt()
    {
        return $this->defriend_at;
    }

    /**
     * 拉黑
     * @return mixed
     */
    abstract public function defriend();
}
