package jp.gihyo.mitoma.android.p025gdplist;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.util.ArrayList;

public class CountryProvDAO {
    private ContentResolver resolver;
    public static final String AUTHORITY = "jp.gihyo.mitoma.android.country.provider";
    public static final Uri CONTENT_URI =
            Uri.parse("content://" + AUTHORITY + "/country");
    private static CountryProvDAO instance = null;
    private CountryProvDAO(Context context){
        this.resolver = context.getContentResolver();
    }
    public static CountryProvDAO getInstance(Context context){
        if(CountryProvDAO.instance == null){
            CountryProvDAO.instance = new CountryProvDAO(context);
        }
        return CountryProvDAO.instance;
    }

    public ArrayList<CountryDTO> select(){
        ArrayList<CountryDTO> list = new ArrayList<>();
        String orderBy ="gdp DESC";
        Cursor cursor = resolver.query(CONTENT_URI, 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 = resolver.query(CONTENT_URI, columns, where,
                selectionArgs, 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){
        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);
        Uri uri = resolver.insert(CONTENT_URI,  values);
        return ContentUris.parseId(uri);
    }
    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 = resolver.update(CONTENT_URI,values,whereClause, whereArgs  );
        return result;
    }
    public int delete(int cNo){
        int result = 0;
        String whereClause = "country_no = ?";
        String[] whereArgs = {String.valueOf(cNo)};
        result = resolver.delete(CONTENT_URI,whereClause, whereArgs);
        return result;
    }

}
