package com.oo.eye.db;

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

import com.app.baselibrary.commonUtil.CheckUtil;
import com.oo.eye.bean.StudentBean;
import com.oo.eye.gen.DaoMaster;
import com.oo.eye.gen.DaoSession;
import com.oo.eye.gen.StudentBeanDao;

import java.util.ArrayList;
import java.util.List;

/**
 */

public class DbManager {

    private static DbManager mDbManager;

    private static final String DB_NAME = "eye_test.db";
    private static DaoMaster.DevOpenHelper mDevOpenHelper;
    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;

    public static DbManager getInstance(Context context) {
        if (null == mDbManager) {
            synchronized (DbManager.class) {
                if (null == mDbManager)
                    mDbManager = new DbManager(context);
            }
        }

        return mDbManager;
    }

    private DbManager(Context context) {
        mDevOpenHelper = new DaoMaster.DevOpenHelper(context,DB_NAME);
        getDaoMaster(context);
        getDaoSession(context);
    }

    /**
     * @desc 获取可读数据库
     * @autor Tiany
     * @time 2016/8/13
     **/
    public static SQLiteDatabase getReadableDatabase(Context context) {
        if (null == mDevOpenHelper) {
            getInstance(context);
        }
        return mDevOpenHelper.getReadableDatabase();
    }

    /**
     * @desc 获取可写数据库
     * @autor Tiany
     * @time 2016/8/13
     **/
    public static SQLiteDatabase getWritableDatabase(Context context) {
        if (null == mDevOpenHelper) {
            getInstance(context);
        }
        return mDevOpenHelper.getWritableDatabase();
    }


    /**
     * 获取DaoSession;
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(Context context) {

        if (null == mDaoSession) {
            synchronized (DbManager.class) {
                if (null == mDaoSession)
                    mDaoSession = getDaoMaster(context).newSession();
            }
        }
        return mDaoSession;
    }

    /**
     * 获取DaoMaster;
     * @param context
     * @return
     */
    public static DaoMaster getDaoMaster(Context context) {

        if (null == mDaoMaster) {
            synchronized (DbManager.class) {
                if (null == mDaoMaster)
                    mDaoMaster = new DaoMaster(getWritableDatabase(context));
            }
        }
        return mDaoMaster;
    }

    /**
     * 获取班级名称
     * @param context
     * @return
     */
    public List<String> getClasses(Context context){
        DbManager.getDaoSession(context).getStudentBeanDao().queryBuilder().list();
        List<String> classes = new ArrayList<>();
        Cursor cursor = DbManager.getWritableDatabase(context)
                .rawQuery("select distinct(CLASSES) FROM STUDENT_BEAN GROUP BY CLASSES",null);
        if(cursor != null){
            for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()) {
                String nameColumn = cursor.getString(0);
                if(!CheckUtil.isEmpty(nameColumn)){
                    classes.add(nameColumn);
                }
            }
        }
        return classes;
    }

    /**
     * 获取学生
     * @param context
     * @param className
     * @return
     */
    public List<StudentBean> getStudents(Context context,String className,boolean isA){
        List<StudentBean> studentBeans = new ArrayList<>();
        try {
            if(!isA) {
                if (!CheckUtil.isEmpty(className)) {
                    studentBeans = DbManager.getDaoSession(context).getStudentBeanDao()
                            .queryBuilder().where(StudentBeanDao.Properties.Classes.eq(className))
                            .build().list();
                } else {
                    studentBeans = DbManager.getDaoSession(context).getStudentBeanDao()
                            .queryBuilder()
                            .build().list();
                }
            }else{
                if (!CheckUtil.isEmpty(className)) {
                    studentBeans = DbManager.getDaoSession(context).getStudentBeanDao()
                            .queryBuilder().where(StudentBeanDao.Properties.Classes.eq(className),
                                    StudentBeanDao.Properties.Left_eye.ge(5.0) ,
                                    StudentBeanDao.Properties.Right_eye.ge(5.0))
                            .build().list();
                } else {
                    studentBeans = DbManager.getDaoSession(context).getStudentBeanDao()
                            .queryBuilder().where(
                                    StudentBeanDao.Properties.Left_eye.ge(5.0) ,
                                    StudentBeanDao.Properties.Right_eye.ge(5.0))
                            .build().list();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return studentBeans;
    }

}
