<?php

declare(strict_types=1);

namespace app\common\library\scrm\spider;

use app\common\library\scrm\AbstractScrmSpider;
use app\common\library\scrm\UnifiedScrmData;

/**
 * 火箭云控蜘蛛
 */
class HuojianSpider extends AbstractScrmSpider
{
    private const API_LIST = '/prod-api1/biz/counter/link/share/';

    private string $pageUrl;

    private string $account;

    private string $password;

    private UnifiedScrmData $unifiedData;

    public function __construct(
        string $pageUrl,
        string $account = '',
        string $password = '',
        string $nodeHost = 'http://127.0.0.1:3001'
    ) {
        parent::__construct($nodeHost);
        $this->pageUrl = $pageUrl;
        $this->account = $account;
        $this->password = $password;
        $this->unifiedData = new UnifiedScrmData();
    }

    protected function getSpiderConfig(): array
    {
        return [
            'pageUrl'        => $this->pageUrl,
            'listApi'        => self::API_LIST,
            'listMethod'     => 'POST',
            'paginationMode' => self::MODE_UI,
            'authActions'    => [
                ['type' => 'vue_fill', 'selector' => '.el-message-box__input input', 'value' => $this->password],
                ['type' => 'wait', 'ms' => 500],
                ['type' => 'vue_click', 'selector' => '.el-message-box__btns .el-button--primary'],
                ['type' => 'wait', 'ms' => 2000],
            ],
        ];
    }

    protected function extractListTotalPages($listFirstPageData, $countData = null)
    {
        return 1;
    }

    protected function buildListPageParams(int $page): array
    {
        return [];
    }

    protected function getUiPaginationConfig(): array
    {
        return [];
    }

    protected function parseToUnifiedData($detailData, array $allListPagesData): UnifiedScrmData
    {
        $unifiedData = $this->unifiedData;
        $unifiedData->todayNewCount = (int) ($allListPagesData[0]['data']['counterWorker']['newTodayFriend'] ?? 0);
        $count = 0;
        foreach ($allListPagesData as $pageRaw) {
            $records = $pageRaw['data']['counterCsAccountVo'] ?? [];
            foreach ($records as $item) {
                if (empty($item['accountLogin'])) {
                    continue;
                }
                $number = (string) $item['accountLogin'];
                $isOnline = isset($item['accountStatus']) && (int) $item['accountStatus'] === 1;
                $count++;
                $unifiedData->addNumber($number, $isOnline, (int) ($item['newTodayFriend'] ?? 0));
            }
        }
        $unifiedData->total = $count;
        return $unifiedData;
    }
}
