package jp.gihyo.mitoma.android.p014gdpmanagement;

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

import java.util.ArrayList;

public class CountryDAO {
    private SQLiteDatabase db;
    private String table = "country";
    private static CountryDAO instance = null;
    private CountryDAO(Context context){
        CountryOpenHelper helper = new CountryOpenHelper(context);
        this.db = helper.getWritableDatabase();
    }
    public static CountryDAO getInstance(Context context){
        if(CountryDAO.instance == null){
            CountryDAO.instance = new CountryDAO(context);
        }
        return CountryDAO.instance;
    }
    public ArrayList<CountryDTO> select(){
        ArrayList<CountryDTO> list = new ArrayList<>();
        String orderBy ="gdp DESC";
        Cursor cursor = db.query(this.table, null, null, null, null, null, orderBy);
        while(cursor.moveToNext()){
            int cNo = cursor.getInt(0);
            String name = cursor.getString(1);
            double gdp = cursor.getDouble(2);
            double pop = cursor.getDouble(3);
            CountryDTO  dto = new CountryDTO(cNo,name,gdp,pop);
            list.add(dto);
        }
        cursor.close();
        return list;
    }

    public CountryDTO select(int cNo){
        CountryDTO dto = null;
        String[] columns ={"country_no","country_name","gdp","population"};
        String where = "country_no=?";
        String[] selectionArgs ={String.valueOf(dto.cNo)};
        Cursor cursor = db.query(table, columns, where,
                selectionArgs, null, null, null);
        if(cursor.moveToFirst()){
            String name = cursor.getString(1);
            Double gdp = cursor.getDouble(2);
            double pop = cursor.getDouble(3);
            dto = new CountryDTO(cNo,name,gdp,pop);
        }
        cursor.close();
        return dto;
    }
    public long insert(CountryDTO dto){
        long ret;
        ContentValues values = new ContentValues();
        values.put("country_no", dto.cNo);
        values.put("country_name", dto.countryName);
        values.put("gdp", dto.gdp);
        values.put("population", dto.population);
        ret = db.insert(table, null, values);
        return ret;
    }
    public int update(CountryDTO dto){
        int result = 0;
        ContentValues values = new ContentValues();
        values.put("country_name", dto.countryName);
        values.put("gdp", dto.gdp);
        values.put("population", dto.population);
        String whereClause = "country_no = ?";
        String[] whereArgs = {String.valueOf(dto.cNo)};
        result = db.update(table,values,whereClause, whereArgs  );
        return result;
    }
    public int delete(int cNo){
        int result = 0;
        String whereClause = "country_no = ?";
        String[] whereArgs = {String.valueOf(cNo)};
        result = db.delete(table,whereClause, whereArgs);
        return result;
    }

}
