<?php


namespace Tests\Services\CacheShelfService;


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

class ProcessTest extends TestCase
{
    use TestMockSubServices;

    /** @var CacheShelfService $cacheShelfService */
    public $cacheShelfService;

    /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
    public $foreignHaiRoboticsService;

    public $data = [];

    protected function setUp(): void
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->cacheShelfService = $this->subMock([
            'class' => CacheShelfService::class,
            'methods' => [
                '_stationCacheLightOn' => new MaterialBox(['code' => 200])
            ],
            'subService' => [
                'serviceName' => 'foreignHaiRoboticsService',
                'class' => ForeignHaiRoboticsService::class,
                'methods' => [
                    'controlHaiRobot' => true
                ]
            ]
        ]);
        $this->foreignHaiRoboticsService = app(ForeignHaiRoboticsService::class);
        $this->data['localCode'] = 'HAITEST-01-01';
        $this->data['boxCode'] = 'IDE0000198';
        $stationType = StationType::query()->firstOrCreate(['name' => '立库']);
        StationType::query()->firstOrCreate(['name' => '缓存架']);
        $this->data['station'] = Station::query()->firstOrCreate(['name' => '立库', 'station_type_id' => $stationType['id']]);
    }

    public function testProcess()
    {

        $result = $this->cacheShelfService->createStationTask($this->data['localCode'], $this->data['boxCode']);
        $this->assertTrue($result['success'], '当前站已有为完成的任务');

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

        // 模拟海柔拍灯
        $this->cacheShelfService->lightOffTask($this->data['localCode'], 0);
        $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
        $this->assertEquals($stationTaskMaterialBoxes->first() ? $stationTaskMaterialBoxes->first()['materialBox']['code'] : null, $this->data['boxCode']);

        /** @var Station $station */
        $station = Station::query()->with(['currentStationTask', 'pendingStationTask'])->where('code', $this->data['localCode'])->first();

        $pendingStationTask = $station->pendingStationTask;

        $this->assertTrue($pendingStationTask ? true : false);
        $this->assertEquals('待处理', $pendingStationTask->status ?? '');

        $materialBox = MaterialBox::query()->where('code', $this->data['boxCode'])->first();
        $this->assertNotEmpty($materialBox);

        $putStationTaskMaterialBox = StationTaskMaterialBox::query()->where('station_id', $this->data['station']['id'])->where('material_box_id', $materialBox['id'])->first();
        $this->assertTrue($putStationTaskMaterialBox ? true : false);
        // 模拟海柔机器人放箱完成
        $result = $this->foreignHaiRoboticsService->taskUpdate($putStationTaskMaterialBox['id'], 1, 0, $this->data['boxCode']);
        $this->assertEquals(true, $result);
        $station = Station::query()->where('code', $this->data['localCode'])->first();
        $this->assertTrue($station ? true : false);

        // 料箱任务
        $putStationTaskMaterialBox->refresh();
        $this->assertEquals('完成', $putStationTaskMaterialBox->status);

        $taskStationTaskMaterialBox = StationTaskMaterialBox::query()->where('station_id', $station['id'])->first();
        $this->assertEquals('完成', $taskStationTaskMaterialBox->status);

        // 栈任务
        $this->assertEquals('完成', $taskStationTaskMaterialBox->stationTask->status);
        $this->assertEquals('完成', $putStationTaskMaterialBox->stationTask->status);
    }

    protected function tearDown(): void
    {
//        $station = Station::query()->with('parent')->where('code',$this->data['localCode'])->first();
//        $materialBox = MaterialBox::query()->where('code',$this->data['boxCode'])->first();
//        if($materialBox){
//            $StationTaskMaterialBoxes = StationTaskMaterialBox::query()->where('material_box_id',$materialBox['id'])->get();
//            foreach ($StationTaskMaterialBoxes as $stationTaskMaterialBox) {
//                if($stationTaskMaterialBox->stationTask) {
//                    StationTaskChildren::query()->where('station_task_id',$stationTaskMaterialBox->stationTask['id'])->delete();
//                    $stationTaskMaterialBox->stationTask ? $stationTaskMaterialBox->stationTask->delete() : null;
//                }
//                $stationTaskMaterialBox->delete();
//            }
//        }
//        if($station){
//            $station->parent->delete();
//            $station->delete();
//        }
        parent::tearDown(); // TODO: Change the autogenerated stub
    }


}
