package com.gyee.wisdom.dao.taoscz;

import com.gyee.wisdom.common.data.timeseries.*;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
public class TaskCallableList implements Callable {

    private Connection connection;
    private String sql;
    private TsDataType type;
    private long startTs;

    public TaskCallableList(Connection connection, String sql, TsDataType type, long startTs) {
        this.connection = connection;
        this.sql = sql;
        this.type = type;
        this.startTs = startTs;
    }


    @Override
    public List<TsData> call() throws SQLException {
        Statement st = null;
        ResultSet rs = null;

        List<TsData> tsDataList = new ArrayList<>();
        try {
            st = this.connection.createStatement();
            rs = st.executeQuery(this.sql);

            long timeShift = 0;
            while (rs.next()) {
                long ts = rs.getLong(1);
                if (timeShift==0) {
                    timeShift = this.startTs - ts;
                }
                ts += timeShift;
                TsData tsData = null;
                if (this.type == TsDataType.DOUBLE) tsData = new DoubleTsData(ts, (short) 0, rs.getDouble(2));
                else if (this.type == TsDataType.BOOLEAN) tsData = new BooleanTsData(ts, (short) 0, rs.getBoolean(2));
                else if (this.type == TsDataType.STRING) tsData = new StringTsData(ts, (short) 0, rs.getString(2));
                else if (this.type == TsDataType.LONG) tsData = new LongTsData(ts, (short) 0, rs.getLong(2));

                if (tsData == null){
                    if (this.type == TsDataType.DOUBLE)
                        tsData = new DoubleTsData(System.currentTimeMillis(), (short) 0, 0);
                    else if (this.type == TsDataType.BOOLEAN)
                        tsData = new BooleanTsData(System.currentTimeMillis(), (short) 0, false);
                    else if (this.type == TsDataType.STRING)
                        tsData = new StringTsData(System.currentTimeMillis(), (short) 0, "0");
                    else if (this.type == TsDataType.LONG)
                        tsData = new LongTsData(System.currentTimeMillis(), (short) 0, 0);
                }

                tsDataList.add(tsData);
            }

        } catch (Exception e) {
            log.error(e.getMessage());
            log.error(this.sql);
            create(this.sql, st);
        } finally {
            if (rs != null)
                rs.close();
            if (st != null)
                st.close();
            if (this.connection != null)
                this.connection.close();
        }

        return tsDataList;
    }

    private void create(String sql, Statement st) throws SQLException {
        String name = sql.substring(sql.lastIndexOf("from") + 5).trim();

        String dbname = name.substring(0, name.indexOf("."));
        String stbname = name.substring(name.indexOf(".") + 1, name.indexOf("_"));
        String ztbname = name.substring(name.indexOf(".") + 1);

        st.execute("CREATE TABLE IF NOT EXISTS " + dbname + "." + ztbname + " USING " +
                dbname + "." + stbname + " TAGS(\"" + ztbname + "\")");
    }
}
