package net.sharermax.m_news.adapter;

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

import net.sharermax.m_news.network.WebResolve;
import net.sharermax.m_news.support.DatabaseHelper;

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

/**
 * Author: SharerMax
 * Time  : 2015/4/30
 * E-Mail: mdcw1103@gmail.com
 */
public class DatabaseAdapter {
    public static final String CLASS_NAME = "DatabaseAdapter";
    private static final String FIELD_TITLE = WebResolve.FIELD_TITLE;
    private static final String FIELD_URL = WebResolve.FIELD_URL;
    private static final String FIELD_TIME = "star_time";
    private SQLiteDatabase mDataBase;
    private DatabaseHelper mDatabaseHelper;
    private ContentValues mContentValues;
    private List<NewsDataRecord> mRecordList;
    private String mSendData;
    private NewsDataRecord mNewsDataRecord;
    private static DatabaseAdapter sDatabaseAdapter;

    public static DatabaseAdapter getInstance(Context context) {
        if (null == sDatabaseAdapter) {
            sDatabaseAdapter = new DatabaseAdapter(context);
        }
        return sDatabaseAdapter;
    }
    private DatabaseAdapter(Context context) {
        mDatabaseHelper = new DatabaseHelper(context);
        mDataBase = mDatabaseHelper.getWritableDatabase();
        mContentValues = new ContentValues();
        mRecordList = new ArrayList<NewsDataRecord>();
    }

    public void insert (NewsDataRecord record) {
        if (null == record) {
            return;
        }
        mContentValues.clear();
        mContentValues.put(DatabaseHelper.MyBaseColumns.COLUMN_NAME_TITLE, record.title);
        mContentValues.put(DatabaseHelper.MyBaseColumns.COLUMN_NAME_URL, record.url);
        mContentValues.put(DatabaseHelper.MyBaseColumns.COLUMN_NAME_TIME, record.time);
        mDataBase.insert(DatabaseHelper.MyBaseColumns.TABLE_NAME, null, mContentValues);
    }

    public void insert() {
        insert(mNewsDataRecord);
    }

    public List<NewsDataRecord> queryAllData() {
        mRecordList.clear();
        String[] projection = {
                DatabaseHelper.MyBaseColumns._ID,
                DatabaseHelper.MyBaseColumns.COLUMN_NAME_TITLE,
                DatabaseHelper.MyBaseColumns.COLUMN_NAME_URL,
                DatabaseHelper.MyBaseColumns.COLUMN_NAME_TIME
        };
        Cursor result = mDataBase.query(DatabaseHelper.MyBaseColumns.TABLE_NAME, projection,
                null, null, null, null, null);
        int count = result.getCount();
        Log.v(CLASS_NAME, count + "");
        result.moveToFirst();
        for (int i = 0; i<count; i++) {
            NewsDataRecord record = new NewsDataRecord();
            record._id = result.getLong(result.getColumnIndex(DatabaseHelper.MyBaseColumns._ID));
            record.title =result.getString(result.getColumnIndex(DatabaseHelper.MyBaseColumns.COLUMN_NAME_TITLE));
            record.url = result.getString(result.getColumnIndex(DatabaseHelper.MyBaseColumns.COLUMN_NAME_URL));
            record.time = result.getLong(result.getColumnIndex(DatabaseHelper.MyBaseColumns.COLUMN_NAME_TIME));
            mRecordList.add(record);
            result.moveToNext();
        }
        result.close();
        return mRecordList;
    }

    public static void close() {
        if (null == sDatabaseAdapter) {
            return;
        }

        SQLiteDatabase db = sDatabaseAdapter.mDataBase;
        if (null != db && db.isOpen()) {
            db.close();
        }
    }

    public void setItemRecord(NewsDataRecord record) {
        mNewsDataRecord = record;
    }

    public NewsDataRecord getItemRecord() {
        return mNewsDataRecord;
    }

    public void setSendData(String sendData) {
        mSendData = sendData;
    }

    public String getSendData() {
        return mSendData;
    }

    public static class NewsDataRecord{
        public long _id = 0;
        public String title = "";
        public String url = "";
        public long time = 0;
    }
 }

@@ -1184,32 +1184,203 @@
 apter(context);%0A
+            Log.v(CLASS_NAME, %22null%22);%0A        %7D else %7B%0A            if (!sDatabaseAdapter.getDataBase().isOpen()) %7B%0A                sDatabaseAdapter.open();%0A            %7D%0A
         %7D%0A      
@@ -1673,32 +1673,113 @@
 cord%3E();%0A    %7D%0A%0A
+    public SQLiteDatabase getDataBase() %7B%0A        return this.mDataBase;%0A    %7D%0A%0A%0A
     public void 
@@ -3603,32 +3603,123 @@
 ordList;%0A    %7D%0A%0A
+    public void open() %7B%0A        mDataBase = mDatabaseHelper.getWritableDatabase();%0A    %7D%0A%0A
     public stati
@@ -3809,36 +3809,15 @@
   %7D%0A
-%0A
        
- SQLiteDatabase db =
  sDa
@@ -3839,88 +3839,27 @@
 Data
-Base;%0A        if (null != db && db.isOpen()) %7B%0A            db.close();%0A        %7D
+baseHelper.close();
 %0A   

fix bug. "attempt to re-open an already-closed object: SQLiteDatabase "