package com.hc.lib.store;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yunhaipiaodi on 2017/9/18.
 */

public class LastPlayTimeStore {
    private static LastPlayTimeStore instance = null;
    private TvDB tvDB = null;

    public LastPlayTimeStore(Context context){
        tvDB = TvDB.getInstance(context);
    }

    public static final synchronized LastPlayTimeStore getInstance(Context context){
        if(instance == null){
            instance = new LastPlayTimeStore(context);
        }
        return instance;
    }

    public void onCreate(SQLiteDatabase db){
        String createTableSql = "CREATE VIRTUAL TABLE IF NOT EXISTS " + SearchIndexColumns.tableName
                +" USING fts3 "
                + " ( "
                + SearchIndexColumns.sourceId + " STRING ,"
                + SearchIndexColumns.time + " STRING ,"
                + SearchIndexColumns.insertTime + " DATETIME DEFAULT CURRENT_TIMESTAMP ) ";
        try{
            db.execSQL(createTableSql);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public boolean tableIsExist(SQLiteDatabase db, String tableName){
        boolean result = false;
        if(tableName == null){
            return false;
        }
        Cursor cursor = null;
        try {
            String sql = "select count(*) as c from Sqlite_master  where type ='table' and name ='"+tableName.trim()+"' ";
            cursor = db.rawQuery(sql, null);
            if(cursor.moveToNext()){
                int count = cursor.getInt(0);
                if(count>0){
                    result = true;
                }
            }
            cursor.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return result;
    }

    public void insert(String time,String sourceId){
        delete(sourceId);
        SQLiteDatabase database = tvDB.getWritableDatabase();
        if(!tableIsExist(database,SearchIndexColumns.tableName)){
            return;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(SearchIndexColumns.sourceId,sourceId);
        contentValues.put(SearchIndexColumns.time,time);
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Date nowDate = new Date();
        contentValues.put(SearchIndexColumns.insertTime,dateFormat.format(nowDate));
        database.insert(SearchIndexColumns.tableName,null,contentValues);
    }

    public void delete(String sourceId){
        SQLiteDatabase database = tvDB.getWritableDatabase();
        if(!tableIsExist(database,SearchIndexColumns.tableName)){
            return;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(SearchIndexColumns.sourceId,sourceId);
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Date nowDate = new Date();
        contentValues.put(SearchIndexColumns.insertTime,dateFormat.format(nowDate));
        String[] deleteArgs = { sourceId };
        database.delete(SearchIndexColumns.tableName,SearchIndexColumns.sourceId + " = ?",deleteArgs);
    }

    public void deleteAll(){
        SQLiteDatabase database = tvDB.getWritableDatabase();
        if(!tableIsExist(database,SearchIndexColumns.tableName)){
            return;
        }
        database.delete(SearchIndexColumns.tableName,null,null);
    }

    public String getTimeBySourceId(String sourceId){
        SQLiteDatabase sqLiteDatabase = tvDB.getReadableDatabase();
        if(!tableIsExist(sqLiteDatabase,SearchIndexColumns.tableName)){
            return null;
        }
        Cursor c;
        String sql = "select * from " +SearchIndexColumns.tableName+ " where "+SearchIndexColumns.sourceId+ " = ?";
        String[] args = {sourceId};
        c = sqLiteDatabase.rawQuery(sql,args);



        c.moveToFirst();
        if(c.getCount() == 0){
            return "";
        }

        String time = c.getString(c.getColumnIndexOrThrow(SearchIndexColumns.time));
        c.close();
        return time;
    }

    /* -------------------------------------------------------------------------------------------- */
    /* table columns interface */
    public interface SearchIndexColumns{
        //表名
        String tableName = "last_play_store";

        //字段定义

        String sourceId = "sourceId";

        String time = "time";

        String insertTime = "insert_time";
    }
}
