package com.huatek.busi.ws.mock;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import org.junit.Before;

import com.google.common.collect.Lists;
import com.huatek.busi.ws.client.XydlService;
import com.huatek.busi.ws.client.XydlServiceImplService;
import com.huatek.busi.ws.protocol.CacAttr;
import com.huatek.busi.ws.protocol.CacDataNode;
import com.huatek.busi.ws.protocol.CacMonitorData;
import com.huatek.busi.ws.protocol.CacRequest;
import com.huatek.torch.util.uuid.Uuid;
import com.thoughtworks.xstream.XStream;

public class AbstractMockTest {
    protected static final XStream XSTREAM;
    static {
        XSTREAM = new XStream();
        // XSTREAM = new XStream(new DomDriver());
        // XSTREAM = new XStream(new StaxDriver());
        // 使用注解
        XSTREAM.processAnnotations(CacRequest.class);
        // 去掉容器——集合“剥皮”
        XSTREAM.addImplicitCollection(CacMonitorData.class, "cacDataNode");
    }

    protected XydlService client;
    protected DecimalFormat df = new DecimalFormat("##########0.########");

    @Before
    public void init() {
        this.client = new XydlServiceImplService().getXydlServiceImplPort();
    }

    protected CacRequest newRequeset(String sensorCode, String type, LocalDate day, String phase) {
        CacRequest request = new CacRequest();

        CacMonitorData cacMonitorData = new CacMonitorData();
        cacMonitorData.setCacid("26M00090990000987");
        cacMonitorData.setDataNodeNum(1);

        CacDataNode cacDataNode = new CacDataNode();
        cacDataNode.setSensorId(sensorCode);
        cacDataNode.setType(type);
        cacDataNode.setEquipmentId("97M00090990000997");
        cacDataNode.setTimeStamp(randomTime(day));

        List<CacAttr> attrs = Lists.newArrayList();

        cacDataNode.setAttrs(attrs);
        cacMonitorData.setCacDataNode(Arrays.asList(cacDataNode));
        request.setCacMonitorData(cacMonitorData);
        return request;
    }

    protected void addAttr(CacRequest request, String name, String val) {
        request.getCacMonitorData().getCacDataNode().get(0).getAttrs().add(new CacAttr(name, val, "FALSE"));
    }

    protected LocalDateTime randomTime(LocalDate date) {
        int hour = randomInt(0, 23);
        int minute = randomInt(0, 59);
        int second = randomInt(0, 59);
        return LocalDateTime.of(date, LocalTime.of(hour, minute, second));
    }

    protected int randomInt(int min, int max) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        return random.ints(min, max + 1).findFirst().getAsInt();
    }

    protected double[] randomVal(long streamSize, double randomNumberOrigin, double randomNumberBound) {
        Random r = new Random(Uuid.getUUID().hashCode());
        return r.doubles(streamSize, randomNumberOrigin, randomNumberBound).toArray();
    }

    protected String[] randomVal(long streamSize) {
        String[] strs = new String[(int)streamSize];
        double[] vals = randomVal(streamSize, 0, 1000.99999999);
        for (int i = 0; i < strs.length; i++) {
            strs[i] = df.format(vals[i]);
        }
        return strs;
    }
}