<?php


namespace Tests\Services\CacheShelfService;


use App\MaterialBox;
use App\Services\CacheShelfService;
use App\Services\ForeignHaiRoboticsService;
use App\Services\StationTaskMaterialBoxService;
use App\Services\StationTaskService;
use App\Station;
use App\StationTask;
use App\StationTaskMaterialBox;
use App\Traits\TestMockSubServices;
use Tests\TestCase;

class CreateStationTask extends TestCase
{
    use TestMockSubServices;

    /** @var CacheShelfService $service */
    protected $service;
    /** @var StationTaskService $stationTaskService */
    protected $stationTaskService;

    protected $data = [];

    /**
     * createStationTask
     * 注册站任务
     * 注册站料箱任务
     * 注册站任务子任务
     */
    protected function setUp(): void
    {
        parent::setUp();
        $this->service = $this->subMock([
            'class' => CacheShelfService::class,
            'methods' => [
                '_stationCacheLightOn' => new MaterialBox(['code' => 200])
            ],
            'subService' => [[
                'serviceName' => 'foreignHaiRoboticsService',
                'class' => ForeignHaiRoboticsService::class,
                'methods' => [
                    'controlHaiRobot' => true
                ]
            ], [
                'serviceName' => 'stationTaskService',
                'class' => StationTaskService::class
            ], [
                'serviceName' => 'stationTaskMaterialBoxService',
                'class' => StationTaskMaterialBoxService::class,
            ]
            ]
        ]);
        $this->stationTaskService = $this->subMock([
            'class' => StationTaskService::class,
        ]);
        $this->data['parentStation'] = factory(Station::class)->create();
        $this->data['station'] = factory(Station::class)->create(['parent_id' => $this->data['parentStation']['id']]);
        $this->data['materialBox'] = factory(MaterialBox::class)->create();
    }

    public function testCreatStationTask()
    {
        $data = $this->service->createStationTask($this->data['station']['code'], $this->data['materialBox']['code']);
        $this->assertTrue($data['success']);

        $stationTask = StationTask::query()->where('station_id', $this->data['station']['id'])->with('stationTaskMaterialBoxes')->first();
        $stationTaskMaterialBox = StationTaskMaterialBox::query()->with('materialBox')->where('station_id', $this->data['station']['id'])->first();
        $this->assertEquals($stationTask['status'], '待处理');
        $this->assertEquals($stationTaskMaterialBox['materialBox']['code'], $this->data['materialBox']['code']);

        $station = Station::query()->with(['pendingStationTask.stationTaskMaterialBoxes'])->where('id', $this->data['station']['id'])->first();
        $this->assertEquals($station['pendingStationTask']['stationTaskMaterialBoxes']->first()['station_id'], $stationTask['station_id']);
    }

    public function testStationTaskIsExists()
    {
        $this->data['stationTask'] = $this->stationTaskService->create(1);
        $this->stationTaskService->registerStations($this->data['stationTask'], [$this->data['station']['id']]);

        $data = $this->service->createStationTask($this->data['station']['code'], $this->data['materialBox']['code']);
        $this->assertFalse($data['success']);
    }


    protected function tearDown(): void
    {
        Station::query()->where('id', $this->data['station']['id'])->delete();
        Station::query()->where('id', $this->data['parentStation']['id'])->delete();
        StationTask::query()->where('station_id', $this->data['station']['id'])->delete();
        MaterialBox::query()->where('id', $this->data['materialBox']['id'])->delete();
        StationTaskMaterialBox::query()->where('station_id', $this->data['station']['id'])->delete();
        if ($this->data['stationTask'] ?? false) StationTask::query()->whereIn('id', data_get($this->data['stationTask'], '*.id'))->delete();
        parent::tearDown();
    }
}
