package com.gh.gamecenter.db;

import android.content.Context;

import androidx.annotation.Nullable;

import com.gh.gamecenter.db.info.GameTrendsInfo;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;

/**
 * Created by khy on 9/01/18.
 */

public class GameTrendsDao {

    private DatabaseHelper helper;
    private Dao<GameTrendsInfo, String> dao;

    public GameTrendsDao(Context context) {
        try {
            helper = DatabaseHelper.getHelper(context);
            dao = helper.getDao(GameTrendsInfo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Nullable
    public GameTrendsInfo findGameTrendsInfo(String userId) {
        try {
            GameTrendsInfo info = dao.queryForId(userId);
            if (info != null) {
                return info;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public long findReadPostTime(String userId) {
        try {
            GameTrendsInfo gameTrendsInfo = dao.queryForId(userId);
            if (gameTrendsInfo != null) {
                return gameTrendsInfo.getReadPostTime();
            }
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public long findInternetPostTime(String userId) {
        try {
            GameTrendsInfo gameTrendsInfo = dao.queryForId(userId);
            if (gameTrendsInfo != null) {
                return gameTrendsInfo.getInternetPostTime();
            }
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public void add(GameTrendsInfo info) {
        try {
            dao.deleteById(info.getUserId());
            dao.create(info);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
