<?php

namespace App\Jobs;

use App\Models\Company;
use App\Models\Member;
use App\Models\SmsBatch;
use App\Models\SmsRule;
use App\Validators\Rules\MobileRule;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class SmsBatchJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * @var SmsBatch
     */
    private $smsBatch;

    /**
     * Create a new job instance.
     *
     * @param SmsBatch $smsBatch
     */
    public function __construct(SmsBatch $smsBatch)
    {
        $this->smsBatch = $smsBatch;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $status = $this->getRuleStatus($this->smsBatch->template->alias);
        if ($status) {
            if ($this->smsBatch->mobiles) {
                $mobiles=explode(",", $this->smsBatch->mobiles);
                foreach ($mobiles as $mobile) {
                    if (validator_check($mobile, new MobileRule())) {
                        $this->dispatchSms($mobile);
                        $this->smsBatch->target_num++;
                    }
                    $this->smsBatch->save();
                }
            } else {
                $mobiles=$this->getMobiles($this->smsBatch->accept_member, $this->smsBatch->accept_type, $this->smsBatch->accept_time);
                if (!empty($mobiles)) {
                    foreach ($mobiles as $mobile) {
                        if (validator_check($mobile, new MobileRule())) {
                            $this->dispatchSms($mobile);
                            $this->smsBatch->target_num++;
                        }
                        $this->smsBatch->save();
                    }
                }
            }
        }
        if ($this->smsBatch->target_num == 0) {
            $this->smsBatch->status=1;
            $this->smsBatch->save();
        }
    }

    protected function getMobiles($accept_member, $accept_type, $accetp_time)
    {
        $where=[];
        if ($accept_type) {
            $where[]=[$accept_type>2?"mobile_audit":"email_audit",$accept_type%2];
        }
        if ($accetp_time) {
            $where[]=["last_login_time",'<',time()-($accetp_time==1?7:30)*24*60*60];
        }
        $memberMobiles=Member::where($where)->pluck('mobile');
        $companyMobiles=Company::where($where)->pluck('mobile');
        if ($accept_member == 0) {
            return $memberMobiles->concat($companyMobiles);
        } else {
            return $accept_member==1?$companyMobiles:$memberMobiles;
        }
    }

    protected function dispatchSms($mobile)
    {
        $smsJob=new SmsJob($mobile, $this->smsBatch->template->alias, []);
        $smsJob->setBatchId($this->smsBatch->id);
        dispatch($smsJob);
    }

    protected function getRuleStatus($alias)
    {
        $result = SmsRule::where(['alias'=>$alias])->first();
        if ($result) {
            if ($result->status) {
                return true;
            }
            return false;
        } else {
            return false;
        }
    }
}