<?php


namespace Tests\Services\CacheShelfService;

use App\MaterialBox;
use App\Services\CacheShelfService;
use App\Services\ForeignHaiRoboticsService;
use App\Station;
use App\StationTask;
use App\StationTaskChildren;
use App\StationTaskMaterialBox;
use App\StationType;
use App\Traits\TestMockSubServices;
use Tests\TestCase;


class LightOffTaskTest extends TestCase
{
    use TestMockSubServices;

    /** @var CacheShelfService $service */
    protected $service;
    /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService  */
    protected $foreignHaiRoboticsService;

    protected $data = [];

    protected function setup(): void
    {
        parent::setup();
        $this->service = $this->subMock([
            'class' => CacheShelfService::class,
            'subService' => [
                'serviceName' => 'foreignHaiRoboticsService',
                'class' => ForeignHaiRoboticsService::class,
                'methods'=>[
                    'controlHaiRobot' =>true
                ]
            ]
        ]);
        $row = 2;
        $col = 1;
        $stationType = StationType::query()->firstOrCreate(['name' => '立库']);
        $this->data['station1'] = factory(Station::class)->create(['station_type_id'=>$stationType['id']]);
        $this->data['parentStation'] = factory(Station::class)->create();
        $this->data['locCode'] = 'HAI'.$this->data['parentStation']['code'].'-0'.$col.'-0'.$row;
        $this->data['station'] = factory(Station::class)->create(['parent_id'=>$this->data['parentStation']['id'],'code' => $this->data['locCode']]);
        $this->data['materialBox'] = factory(MaterialBox::class)->create();

        $this->data['stationTask'] = factory(StationTask::class)->create();
        $this->data['stationTask']['station_id'] = $this->data['station']['id'];
        $this->data['stationTask']->save();

        $this->data['stationTaskMaterialBox'] = factory(StationTaskMaterialBox::class)->create([
            'station_id' => $this->data['station']['id'],
            'material_box_id' => $this->data['materialBox']['id'],
            'status' => '待处理'
        ]);

        $this->data['stationTaskMaterialBox']['station_task_id'] = $this->data['stationTask']['id'];
        $this->data['stationTaskMaterialBox']->save();

        $this->data['stationTaskChildren']  = StationTaskChildren::query()->create([
            "station_task_id" => $this->data['stationTask']['id'],
        ]);
        $this->data['stationTaskChildren']["station_taskable_type"] = StationTaskMaterialBox::class;
        $this->data['stationTaskChildren']["station_taskable_id"]= $this->data['stationTaskMaterialBox']['id'];
        $this->data['stationTaskChildren']->save();
        $this->data['PTLAction'] = 0;
    }

    public function testLightOffTask()
    {
        $this->service->lightOffTask($this->data['locCode'],$this->data['PTLAction']);
        $task = StationTaskMaterialBox::query()->where('station_id',$this->data['station']['id'])->where('material_box_id',$this->data['materialBox']['id'])->first();
        $this->assertNotEmpty($task);
        $this->assertEquals($task['status'],'处理中');
    }

    protected function tearDown(): void
    {
        $materialBox = MaterialBox::query()->where('id', $this->data['materialBox']['id'])->first();
        if ($materialBox) {
            $stationTaskMaterialBoxes = StationTaskMaterialBox::query()->where('material_box_id', $materialBox['id'])->get();
            foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
                if($stationTaskMaterialBox->station->parent)$stationTaskMaterialBox->station->parent->delete();
                if ($stationTaskMaterialBox->station) $stationTaskMaterialBox->station->delete();
                if ($stationTaskMaterialBox->stationTask) $stationTaskMaterialBox->stationTask->delete();
                $stationTaskMaterialBox->delete();
            }
            $materialBox->delete();
        }
        if($this->data['stationTaskChildren'])StationTaskChildren::query()->where('id',$this->data['stationTaskChildren']['id'])->delete();
        if($this->data['station1'])Station::query()->where('id',$this->data['station1']['id'])->delete();
        parent::tearDown();
    }
}
