package com.yutou.tools.sqlite;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;

public class ADSQLiteManager extends SQLiteManager {
    private static ADSQLiteManager manager;

    public static ADSQLiteManager getInstance() {
        if (manager == null) {
            manager = new ADSQLiteManager();
        }
        return manager;
    }

    private ADSQLiteManager() {
        super();
        JSONObject json = JSONObject.parseObject("{\"file\":\"ad.db\",\"table\":[{\"name\":\"app\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"appName\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"packageName\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"appId\",\"type\":\"String\",\"isNull\":false,\"isKey\":false}]},{\"name\":\"app_ad\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"appId\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"show\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"adSrc\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"adAppId\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"adAppKey\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"adOpenKey\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"adInterKey\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"adBannerKey\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"adVideoKey\",\"type\":\"String\",\"isNull\":true,\"isKey\":false},{\"name\":\"adNativeKey\",\"type\":\"String\",\"isNull\":true,\"isKey\":false}]},{\"name\":\"ad_log\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"appId\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"adSrc\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"adType\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"model\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"subtime\",\"type\":\"String\",\"isNull\":false,\"isKey\":false}]}]}");
        build(json);
    }

    public boolean addAdData(String appId, String adSrc, String adAppId, String adAppKey, String adOpenKey, String adInterKey,
                             String adBannerKey, String adVideoKey, String adNativeKey) {
        try {
            if (getAdData(appId) != null) {
                return false;
            }
            Statement statement = conn.createStatement();
            String sql = "insert into `app_ad` (`appId`,`adSrc`,`adAppId`,`adAppKey`,`adOpenKey`,`adInterKey`,`adBannerKey`,`adVideoKey`,`adNativeKey`,`show`) values " +
                    "('" + appId + "','" + adSrc + "','" + adAppId + "','" + adAppKey + "','" + adOpenKey + "','" + adInterKey + "','" + adBannerKey + "','" + adVideoKey + "','" + adNativeKey + "',1);";
            boolean flag = statement.execute(sql);
            statement.closeOnCompletion();
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean addLog(String appId, String src, String type, String model) {
        try {
            Statement statement = conn.createStatement();
            String sql = "insert into `ad_log` (`appId`,`adSrc`,`adType`,`model`,`subtime`) values ('" + appId + "','" + src + "','" + type
                    + "','" + model + "','" + System.currentTimeMillis() + "'); ";
            boolean flag = statement.execute(sql);
            statement.closeOnCompletion();
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String addAppData(String appName, String packageName) {
        try {
            if (getAppOfPackageName(packageName) != null) {
                return getAppOfPackageName(packageName).getString("appId");
            }
            Statement statement = conn.createStatement();
            String appId = UUID.randomUUID().toString();
            String sql = "insert into `app`(`appName`,`packageName`,`appId`) values ('" + appName + "','" + packageName + "','" + appId + "')";
            statement.execute(sql);
            statement.closeOnCompletion();
            return appId;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject getAdData(String appId) {
        try {
            Statement statement = conn.createStatement();
            String sql = "select * from `app_ad` where `appId`='" + appId + "';";
            return getData(statement, sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject getAdDataOfPackageName(String packageName) {

        try {
            Statement statement = conn.createStatement();
            String sql = "select * from `app_ad` where `packageName`='" + packageName + "';";
            return getData(statement, sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject getData(Statement statement, String sql) throws SQLException {
        JSONObject json=null;
        ResultSet set = statement.executeQuery(sql);
        if (set.next()) {
            json=new JSONObject();
            json.put("appId", set.getString("appId"));
            json.put("adSrc", set.getString("adSrc"));
            json.put("show", set.getInt("show"));
            if(json.getInteger("show")==1) {
                json.put("adAppId", set.getString("adAppId"));
                json.put("adAppKey", set.getString("adAppKey"));
                json.put("adOpenKey", set.getString("adOpenKey"));
                json.put("adInterKey", set.getString("adInterKey"));
                json.put("adBannerKey", set.getString("adBannerKey"));
                json.put("adVideoKey", set.getString("adVideoKey"));
                json.put("adNativeKey", set.getString("adNativeKey"));
            }else{
                json.put("adAppId", "");
                json.put("adAppKey", "");
                json.put("adOpenKey", "");
                json.put("adInterKey", "");
                json.put("adBannerKey", "");
                json.put("adVideoKey", "");
                json.put("adNativeKey", "");
            }
        }
        set.close();
        statement.closeOnCompletion();
        return json;
    }

    public JSONArray getLog(String appId, String startTime, String endTime) {
        try {
            JSONArray array = new JSONArray();
            Statement statement = conn.createStatement();
            String sql = "select * from `ad_log` where  `appId`=`" + appId + "` and `subtime` >=" + startTime + " and `subtime` <=" + endTime + ";";
            ResultSet set = statement.executeQuery(sql);
            while (set.next()) {
                JSONObject item = new JSONObject();
                item.put("id", set.getInt("id"));
                item.put("appId", set.getString("appId"));
                item.put("adSrc", set.getString("adSrc"));
                item.put("adType", set.getString("adType"));
                item.put("model", set.getString("model"));
                item.put("subtime", set.getString("subtime"));
                array.add(item);
            }
            set.close();
            statement.closeOnCompletion();
            return array;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public JSONObject getAppOfAppName(String appName){
        try {
            JSONObject json=new JSONObject();
            Statement statement=conn.createStatement();
            String sql="select * from `app` where `appName`='"+appName+"';";
            json=getApp(statement, sql);
            statement.closeOnCompletion();
            return json;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public JSONObject getAppOfPackageName(String packageName){
        try {
            JSONObject json=new JSONObject();
            Statement statement=conn.createStatement();
            String sql="select * from `app` where `packageName`='"+packageName+"';";
            json=getApp(statement, sql);
            statement.closeOnCompletion();
            return json;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public JSONArray getAllApp(){
        try {
            JSONArray array=new JSONArray();
            Statement statement=conn.createStatement();
            String sql="select * from `app` ;";
            ResultSet set=statement.executeQuery(sql);
            while (set.next()){
                JSONObject json=new JSONObject();
                json.put("id",set.getInt("id"));
                json.put("appId",set.getString("appId"));
                json.put("appName",set.getString("appName"));
                json.put("packageName",set.getString("packageName"));
                array.add(json);
            }
            return array;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private JSONObject getApp(Statement statement, String sql) throws SQLException {
        JSONObject json=null;
        ResultSet set=statement.executeQuery(sql);
        if(set.next()){
            json=new JSONObject();
            json.put("id",set.getInt("id"));
            json.put("appId",set.getString("appId"));
            json.put("appName",set.getString("appName"));
            json.put("packageName",set.getString("packageName"));
        }
        set.close();
        statement.closeOnCompletion();
        return json;
    }

    public void close() {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
            conn = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
