package com.epson.mobilephone.common.wifidirect;

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

public class SimpleAPInfoDB extends SQLiteOpenHelper {
    public static final String COLUMN_DEVICEID = "deviceid";
    public static final String COLUMN_SSID = "ssid";
    private static final String CREATEDB = "create table simleapinfo ( ssid text primary key not null, deviceid text not null  );";
    private static final String DB_NAME = getClassName();
    private static final int DB_VERSION = 1;
    public static final String TABLE_SIMPLEAPINFO = "simleapinfo";
    private static final String TAG = "SimpeAPInfoDB";

    public static class SimpleAPInfo {
        String printerName;
        String ssid;
    }

    public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
    }

    public static SimpleAPInfo getSimpleAPInfoDB(Context context, String str) {
        SimpleAPInfo simpleAPInfo;
        SQLiteDatabase readableDatabase = new SimpleAPInfoDB(context).getReadableDatabase();
        Cursor query = readableDatabase.query(TABLE_SIMPLEAPINFO, new String[]{"ssid", COLUMN_DEVICEID}, "ssid=?", new String[]{str}, (String) null, (String) null, (String) null);
        if (query == null || query.getCount() <= 0) {
            simpleAPInfo = null;
        } else {
            EPLog.m88i(TAG, "getSimpleAPInfoDB found info.");
            int columnIndex = query.getColumnIndex("ssid");
            int columnIndex2 = query.getColumnIndex(COLUMN_DEVICEID);
            query.moveToFirst();
            simpleAPInfo = new SimpleAPInfo();
            simpleAPInfo.ssid = query.getString(columnIndex);
            simpleAPInfo.printerName = query.getString(columnIndex2);
            query.close();
        }
        readableDatabase.close();
        return simpleAPInfo;
    }

    public static boolean updateSimpleAPInfoDB(Context context, SimpleAPInfo simpleAPInfo) {
        EPLog.m88i(TAG, "updateSimpleAPInfoDB ssid = " + simpleAPInfo.ssid);
        SQLiteDatabase writableDatabase = new SimpleAPInfoDB(context).getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("ssid", WiFiUtils.removeQuotationsInSSID(simpleAPInfo.ssid));
        contentValues.put(COLUMN_DEVICEID, simpleAPInfo.printerName);
        writableDatabase.replace(TABLE_SIMPLEAPINFO, (String) null, contentValues);
        writableDatabase.close();
        return true;
    }

    private SimpleAPInfoDB(Context context) {
        super(context, DB_NAME, (SQLiteDatabase.CursorFactory) null, 1);
    }

    public void onCreate(SQLiteDatabase sQLiteDatabase) {
        sQLiteDatabase.execSQL(CREATEDB);
    }

    private static String getClassName() {
        try {
            throw new Exception();
        } catch (Exception e) {
            StackTraceElement[] stackTrace = e.getStackTrace();
            return stackTrace != null ? stackTrace[0].getClassName() : "com.epson.iprint.wifidirect.SimpeAPInfoDB";
        }
    }
}
