package com.sobot.common.login.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.sobot.common.login.model.HostModel;
import com.sobot.common.utils.SobotGlobalContext;

import java.util.List;


/**
 * 域名 操作类
 */
public class HostManager extends SobotCommonDao<HostModel> {
    private HostManager(Context context) {
        super(new SobotCommonDBHelper(context));
    }

    //volatile关键字确保多线程下正确处理
    private volatile static HostManager mInstance;

    public static HostManager getInstance() {
        //第一次判空
        if (mInstance == null) {
            //进入同步区域
            synchronized (HostManager.class) {
                //第二次判空
                if (mInstance == null) {
                    mInstance = new HostManager(SobotGlobalContext.getAppContext());
                }
            }
        }
        return mInstance;
    }

    @Override
    public String getTableName() {
        return SobotCommonDBHelper.TABLE_SOBOT_HOST;
    }

    @Override
    public void unInit() {

    }

    @SuppressLint("Range")
    @Override
    public HostModel parseCursorToBean(Cursor cursor) {
        HostModel model = new HostModel();
        model.setDesc(cursor.getString(cursor.getColumnIndex("hostDesc")));
        model.setHost(cursor.getString(cursor.getColumnIndex("host")));
        model.setJanussipProxy(cursor.getString(cursor.getColumnIndex("janussipProxy")));
        model.setJanusSocket(cursor.getString(cursor.getColumnIndex("janusSocket")));
        model.setOpenApiHost(cursor.getString(cursor.getColumnIndex("openApiHost")));
        model.setStompSocket(cursor.getString(cursor.getColumnIndex("stompSocket")));
        return model;
    }

    @Override
    public ContentValues getContentValues(HostModel hostModel) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("hostDesc", hostModel.getDesc());
        contentValues.put("host", hostModel.getHost());
        contentValues.put("janussipProxy", hostModel.getJanussipProxy());
        contentValues.put("janusSocket", hostModel.getJanusSocket());
        contentValues.put("openApiHost", hostModel.getOpenApiHost());
        contentValues.put("stompSocket", hostModel.getStompSocket());
        return contentValues;
    }

    public void saveHost(HostModel host) {
        deleteAll();
        insert(host);
    }

    /**
     * 获取最近用户记录 按日期倒序
     */
    public HostModel getHost() {
        List<HostModel> list = query(null, null, null, null, null, null, null);
        return list.size() > 0 ? list.get(0) : null;
    }
}
