package user.company;

import common.Constant;
import user.common.AddressBind;
import user.common.AddressStatus;
import user.common.BindQuery;
import user.common.BindQueryResp;
import util.Tools;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

public class Test {
    //todo 仅供参考，实际开发的需要自行封装各个接口
    public static void main(String[] args) throws Exception {
        //这三行代码是一次性调用的，获取到平台唯一标识之后放到配置文件就行，后续也不会变化
//        SendVerifyCode sendVerifyCode = new SendVerifyCode(1, "429005199911300039", "樊友祥");
//        sendVerifyCode.sendVerifyCode();
//        QueryUserIdentification queryUserIdentification = new QueryUserIdentification(1, "370105198901010001", "张三", "123456");

        // mock file
        byte[] bytesFile = Tools.getFiles("https://i0.wp.com/www.18hall.com/wp-content/uploads/2023/11/%E8%B2%93cafe.jpg?resize=1000%2C485&ssl=1");
        String tempFileName = String.format("%s_%s.%s", "猫", UUID.randomUUID(), "jpg");
        Path tempFilePath =Paths.get(".", tempFileName);
        Files.write(tempFilePath, bytesFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        File fi = tempFilePath.toFile();

        UploadLicense uploadLicense = new UploadLicense(fi);
        RegisterCompany registerCompany = new RegisterCompany("深圳市腾讯计算机系统有限公司", "91440300708461136T", uploadLicense.UploadLicense(), "马化腾", "樊友祥", "18824307724", "429005199911300039", 1, 3);
        System.out.println(registerCompany.register());
        //delete mock file
        fi.delete();

        AddressBind bd = new AddressBind(registerCompany.register());
        ArrayList<String> list = new ArrayList<>();
        String address = bd.addressBind();
        list.add(address);

        String address1 = bd.addressBind();
        list.add(address1);

        List<AddressStatus> addressStatuses = waitingBind(list);
        System.out.println(addressStatuses);
    }

    public static List<AddressStatus> waitingBind(List<String> addressList) throws Exception {
        List<AddressStatus> resp = new ArrayList<>();
        BindQuery bq = new BindQuery(addressList);

        Duration timeout = Duration.ofMillis(Constant.DEFAULT_TIMEOUT);
        long timeBegin = System.currentTimeMillis();
        while (!bq.getAddressList().isEmpty() && Duration.between(
                Instant.ofEpochMilli(timeBegin),
                Instant.now()).compareTo(timeout) <= 0) {
            BindQueryResp bindQueryResp = bq.bindQuery();

            // 收集已经成功或失败的地址 todo 这里已经成功或失败的应该持久化
            resp.addAll(bindQueryResp.getList().stream()
                    .filter(addressStatus -> addressStatus.getStatus() == 2 || addressStatus.getStatus() == 3)
                    .collect(Collectors.toList()));

            // 查询条件过滤成功或者失败的情况
            List<String> addressList1 = bindQueryResp.getList().stream()
                    .filter(addressStatus -> addressStatus.getStatus() == 1 || addressStatus.getStatus() == 4)
                    .map(AddressStatus::getAddress)
                    .collect(Collectors.toList());

            bq.setAddressList(addressList1);

            // 等待一段时间再去查询
            TimeUnit.SECONDS.sleep(1);
        }

        // todo 日志打印超时后绑定中/未绑定的案例
        System.out.println(bq.getAddressList());

        return resp;
    }
}
