package com.yutou.bilibili.sqlite;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.bilibili.datas.DanmuData;
import com.yutou.common.databases.AbsDatabasesBean;
import com.yutou.common.databases.SQLiteManager;
import com.yutou.common.utils.Log;

import java.io.File;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class BiliBiliLiveDatabasesManager extends SQLiteManager {


    public BiliBiliLiveDatabasesManager() {
        super();

    }

    @Override
    public String getFileName() {
        return "";
    }

    @Override
    protected List<AbsDatabasesBean> getDataBean() {
        return null;
    }

    private JSONObject getInitJSON(){
        return JSONObject.parseObject("{\"file\":\"bilibili.db\",\"table\":[{\"name\":\"danmu\",\"item\":[{\"name\":\"id\",\"type\":\"int\",\"isNull\":false,\"isKey\":true},{\"name\":\"model\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"fontSize\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"fontColor\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"time\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"uCode\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"danmu\",\"type\":\"String\",\"isNull\":false,\"isKey\":false},{\"name\":\"uid\",\"type\":\"int\",\"isNull\":false,\"isKey\":false},{\"name\":\"uname\",\"type\":\"String\",\"isNull\":false,\"isKey\":false}]}]}");
    }
    public void init(String fileName){
        JSONObject json = getInitJSON();
        json.put("model","create");
        json.put("file",fileName+".db");
        //build(json);
    }
    public void init(File file){
        JSONObject json = getInitJSON();
        json.put("model","load");
        json.put("file",file.getAbsolutePath());
       // build(json);
    }

    /**
     * 添加弹幕
     * @param data 弹幕参数
     */
   public void addDanmu(DanmuData data){
        try{
            Statement statement = getConnection().createStatement();
            String sql=String.format("insert into `danmu` (`model`,`fontSize`,`fontColor`,`time`,`uCode`,`danmu`,`uid`,`uname`) " +
                    "values (%d,%d,%d,%d,'%s','%s',%d,'%s')"
                    ,data.getModel()
                    ,data.getFontSize()
                   // ,data.getFontColor()
                    ,data.getTime()
                    ,data.getUCode()
                    ,data.getDanmu()
                    ,data.getUid()
                    ,data.getUname()
            );
            statement.execute(sql);
            statement.closeOnCompletion();
        }catch (Exception e){
           Log.e(e);
        }
   }

    /**
     * 获取弹幕
     * @param startTime 弹幕范围开始时间
     * @param endTime 弹幕范围结束时间
     * @return 弹幕列表
     */
    public List<DanmuData> queryDanmu(Date startTime,Date endTime){
        List<DanmuData> list=new ArrayList<>();
        try {
            String sql=String.format("select * from `danmu` where time >%d and time <%d",startTime.getTime(),endTime.getTime());
            Statement statement = getConnection().createStatement();
            ResultSet set = statement.executeQuery(sql);
            while (set.next()){
                DanmuData data=new DanmuData();
                data.setId(set.getInt("id"));
                data.setModel(set.getInt("model"));
                data.setFontSize(set.getInt("fontSize"));
              //  data.setFontColor(set.getInt("fontColor"));
                data.setTime(set.getLong("time"));
                data.setUCode(set.getString("uCode"));
                data.setDanmu(set.getString("danmu"));
                data.setUid(set.getLong("uid"));
                data.setUname(set.getString("uname"));
                list.add(data);
            }
            set.close();
            statement.closeOnCompletion();;
        }catch (Exception e){
           Log.e(e);
        }
        return list;
    }

}
