package com.gyee.edge.gateway.config.cache;

import com.gyee.edge.gateway.client.EndPoint;
import com.gyee.edge.gateway.config.point.*;
import com.gyee.edge.gateway.config.sqlite.Database;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@Component
public class CacheService {

    @Autowired
    private Database db;

    private List<EndPoint> endPointList;

    private List<Iec102Point> iec102PointList;

    private List<Iec104Point> iec104PointList;

    private List<ModbusPoint> modbusPointList;

    private List<BfPoint> bfPointList;

    private List<BachmanPoint> bachmanPointList;

    private List<GdupcPoint> gdupcPointList;

    public List<EndPoint> getEndPointList() {
        if (endPointList == null) {
            endPointList = loadEndPoints();
        }

        return endPointList;
    }

    public List<Iec102Point> getIec102PointList(){
        if (iec102PointList == null){
            iec102PointList = loadIec102Points();
        }

        return iec102PointList;
    }

    public List<Iec104Point> getIec104PointList(){
        if (iec104PointList == null){
            iec104PointList = loadIec104Points();
        }

        return iec104PointList;
    }

    public List<ModbusPoint> getIModbusPointList(){
        if (modbusPointList == null){
            modbusPointList = loadmodbusPoints();
        }

        return modbusPointList;
    }

    public List<BfPoint> getBfPointList(){
        if (bfPointList == null){
            bfPointList = loadBfPoints();
        }

        return bfPointList;
    }

    public List<BachmanPoint> getBachmanPointList(){
        if (bachmanPointList == null){
            bachmanPointList = loadBachmanPoints();
        }

        return bachmanPointList;
    }

    public List<GdupcPoint> getGdupcPointList(){
        if (gdupcPointList == null){
            gdupcPointList = loadGdupcPoints();
        }

        return gdupcPointList;
    }

    private List<EndPoint> loadEndPoints() {
        // DONE: 从数据库中加载endpoint
        List<EndPoint> endPointList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from endpoint";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                EndPoint endPoint = new EndPoint();
                endPoint.setId(rs.getInt("id"));
                endPoint.setConnectionType(rs.getString("connectionType"));
                endPoint.setDirection(rs.getInt("direction"));
                endPoint.setProtocol(rs.getString("protocol"));
                endPoint.setHost(rs.getString("host"));
                endPoint.setPort(rs.getInt("port"));
                endPoint.setSerialPort(rs.getString("serialPort"));
                endPoint.setBaudRate(rs.getInt("baudRate"));
                endPoint.setStartBit(rs.getInt("startBit"));
                endPoint.setStopBit(rs.getInt("stopBit"));
                endPoint.setCheckBit(rs.getInt("checkBit"));
                endPoint.setUserName(rs.getString("username"));
                endPoint.setPassword(rs.getString("password"));
                endPoint.setToken(rs.getString("token"));
                endPoint.setConnectionString(rs.getString("connectionString"));
                endPoint.setPath(rs.getString("path"));
                endPoint.setEnabled(rs.getBoolean("enabled"));
                endPointList.add(endPoint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.releaseResource();
        }
        return endPointList;
    }

    private List<Iec102Point> loadIec102Points() {
        //DONE: 从数据库中加载Iec102Point
        List<Iec102Point> iec102PointList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from iec102";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Iec102Point iec102Point = new Iec102Point();
                iec102Point.setEndpointid(rs.getInt("endpointid"));
                iec102Point.setGyfp2_base(rs.getFloat("gyfp2_base"));
                iec102Point.setGyfp2_addr(rs.getInt("gyfp2_addr"));
                iec102Point.setGyfp2_coeff(rs.getInt("gyfp2_coeff"));
                iec102Point.setIec102_addr(rs.getInt("iec102_addr"));
                iec102Point.setIec102_base(rs.getFloat("iec102_base"));
                iec102Point.setIec102_coeff(rs.getFloat("iec102_coeff"));
                iec102Point.setGyfp2_valid(rs.getInt("gyfp2_valid"));
                iec102PointList.add(iec102Point);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.releaseResource();
        }
        return iec102PointList;
    }

    private List<Iec104Point> loadIec104Points() {
        //DONE: 从数据库中加载Iec104Point
        List<Iec104Point> iec104PointList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from iec104";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Iec104Point iec104Point = new Iec104Point();
                iec104Point.setEndpointid(rs.getInt("endpointid"));
                iec104Point.setIec104_addr(rs.getInt("iec104_addr"));
                iec104Point.setIec104_xmax(rs.getFloat("iec104_xmax"));
                iec104Point.setIec104_xmin(rs.getFloat("iec104_xmin"));
                iec104Point.setIec104_base(rs.getFloat("iec104_base"));
                iec104Point.setIec104_coeff(rs.getFloat("iec104_coeff"));
                iec104Point.setGyfp2_base(rs.getFloat("gyfp2_base"));
                iec104Point.setGyfp2_addr(rs.getInt("gyfp2_addr"));
                iec104Point.setGyfp2_coeff(rs.getInt("gyfp2_coeff"));
                iec104Point.setGyfp2_valid(rs.getInt("gyfp2_valid"));
                iec104PointList.add(iec104Point);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.releaseResource();
        }
        return iec104PointList;
    }

    private List<ModbusPoint> loadmodbusPoints() {
        //DONE: 从数据库中加载ModbusPoint
        List<ModbusPoint> modbusPointList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from modbus";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ModbusPoint modbusPoint = new ModbusPoint();
                modbusPoint.setEndpointid(rs.getInt("endpointid"));
                modbusPoint.setModbus_uid(rs.getInt("modbusPoint"));
                modbusPoint.setModbus_addr(rs.getInt("modbus_addr"));
                modbusPoint.setModbus_fun(rs.getInt("modbus_fun"));
                modbusPoint.setModbus_swap(rs.getInt("modbus_swap"));
                modbusPoint.setModbus_type(rs.getInt("modbus_type"));
                modbusPoint.setGyfp2_base(rs.getFloat("gyfp2_base"));
                modbusPoint.setGyfp2_addr(rs.getInt("gyfp2_addr"));
                modbusPoint.setGyfp2_coeff(rs.getInt("gyfp2_coeff"));
                modbusPoint.setGyfp2_valid(rs.getInt("gyfp2_valid"));
                modbusPointList.add(modbusPoint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.releaseResource();
        }
        return modbusPointList;
    }

    private List<BfPoint> loadBfPoints() {
        //DONE: 从数据库中加载BfPoint
        List<BfPoint> bfPointList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from bf";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                BfPoint bfPoint = new BfPoint();
                bfPoint.setEndpointid(rs.getInt("endpointid"));
                bfPoint.setBf_netid(rs.getString("bf_netid"));
                bfPoint.setBf_key(rs.getString("bf_key"));
                bfPoint.setGyfp2_base(rs.getFloat("gyfp2_base"));
                bfPoint.setGyfp2_addr(rs.getInt("gyfp2_addr"));
                bfPoint.setGyfp2_coeff(rs.getInt("gyfp2_coeff"));
                bfPoint.setGyfp2_valid(rs.getInt("gyfp2_valid"));
                bfPointList.add(bfPoint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.releaseResource();
        }
        return bfPointList;
    }

    private List<BachmanPoint> loadBachmanPoints() {
        //DONE: 从数据库中加载BachmanPoint
        List<BachmanPoint> bachmanPointList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from bachman";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                BachmanPoint bachmanPoint = new BachmanPoint();
                bachmanPoint.setEndpointid(rs.getInt("endpointid"));
                bachmanPoint.setIp(rs.getString("ip"));
                bachmanPoint.setBachman(rs.getString("bachman"));
                bachmanPoint.setGyfp2_base(rs.getFloat("gyfp2_base"));
                bachmanPoint.setGyfp2_addr(rs.getInt("gyfp2_addr"));
                bachmanPoint.setGyfp2_coeff(rs.getInt("gyfp2_coeff"));
                bachmanPoint.setGyfp2_valid(rs.getInt("gyfp2_valid"));
                bachmanPointList.add(bachmanPoint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            db.releaseResource();
        }
        return bachmanPointList;
    }

    private List<GdupcPoint> loadGdupcPoints() {
        //DONE: 从数据库中加载GdupcPoint
        List<GdupcPoint> gdupcPointList = new ArrayList<>();
        try {
            Connection conn = db.getConnection();
            String sql = "select * from gdupc";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                GdupcPoint gdupcPoint = new GdupcPoint();
                gdupcPoint.setEndpointid(rs.getInt("endpointid"));
                gdupcPoint.setGdupc_ip(rs.getString("gdupc_ip"));
                gdupcPoint.setGdupc_tag(rs.getString("gdupc_tag"));
                gdupcPoint.setGdupc_type(rs.getString("gdupc_type"));
                gdupcPoint.setGdupc_len(rs.getInt("gdupc_len"));
                gdupcPoint.setGyfp2_base(rs.getFloat("gyfp2_base"));
                gdupcPoint.setGyfp2_addr(rs.getInt("gyfp2_addr"));
                gdupcPoint.setGyfp2_coeff(rs.getInt("gyfp2_coeff"));
                gdupcPoint.setGyfp2_valid(rs.getInt("gyfp2_valid"));
                gdupcPointList.add(gdupcPoint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            db.releaseResource();
        }
        return gdupcPointList;
    }

}
