package com.yutou.bilibili.sqlite;

import com.yutou.bilibili.BiliBili.Datas.BiliBiliUpData;
import com.yutou.bilibili.BiliBili.Datas.DanmuData;
import com.yutou.bilibili.BiliBili.Datas.LiveData;
import com.yutou.bilibili.BiliBili.Datas.LiveInfo;
import com.alibaba.fastjson.JSONObject;
import com.yutou.bilibili.BiliBili.LiveUtils;
import com.yutou.bilibili.Tools.AppTools;
import com.yutou.bilibili.Tools.Log;
import com.yutou.bilibili.mybatis.Bili.mybatis.model.BilibiliLiveData;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;

public class BiliBiliLiveDatabasesManager extends SQLiteManager {


    public BiliBiliLiveDatabasesManager() {
        super();

    }
    public void init(String fileName){
        JSONObject json = 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}]}]}");
        json.put("file",fileName+".db");
        build(json);
    }

    /**
     * 添加弹幕
     * @param data 弹幕参数
     */
   public void addDanmu(DanmuData data){
        try{
            Statement statement = conn.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){
            com.yutou.bilibili.Tools.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 = conn.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.getInt("time"));
                data.setUCode(set.getString("uCode"));
                data.setDanmu(set.getString("danmu"));
                data.setUid(set.getInt("uid"));
                data.setUname(set.getString("uname"));
                list.add(data);
            }
            set.close();
            statement.closeOnCompletion();;
        }catch (Exception e){
            com.yutou.bilibili.Tools.Log.e(e);
        }
        return list;
    }

}
