package cn.net.caoyao.cpzshandset.db.dal;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.net.caoyao.cpzshandset.bean.City;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.db.CityDBHelper;

public class CityDal {
    private SQLiteDatabase db;
    private CityDBHelper helper;
    private static String DB_PATH = "/data/data/cn.net.caoyao.cpzshandset/databases/";
    public CityDal(Context context) {
        helper = new CityDBHelper(context, false);
        try {
            helper.createDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        File shm = context.getDatabasePath(DB_PATH + "city.db-shm");
//        File wal = context.getDatabasePath(DB_PATH + "city.db-wal");
//        if (shm.exists()) {
//            shm.delete();
//        }
//        if (wal.exists()) {
//            wal.delete();
//        }
    }

    public City queryCityByCode(int cityCode) {
        City city = null;
        db = helper.getReadableDatabase();
        String sql = "select * from city where id=?";
        Cursor c = db.rawQuery(sql, new String[]{String.valueOf(cityCode)});
        if (c.moveToNext()) {
            city = getCityInfo(c);
        }
        c.close();
        db.close();
        return city;
    }

    /**
     * 湖获取全国的省市名称
     *
     * @return
     */
    public List<City> queryProvince() {
        List<City> cities = new ArrayList<City>();
        try {
            db = helper.getReadableDatabase();
            Cursor cursor = db.rawQuery("SELECT * FROM city WHERE city is null  AND area is null", null);

            while (cursor.moveToNext()) {
                City city = getCityInfo(cursor);
                cities.add(city);
            }
//            if (cities.isEmpty()){
//                try {
//                    helper.createDb();
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//            }
            cursor.close();
            db.close();

            return cities;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cities;
    }

    /**
     * 通过省名称获取城市列表
     *
     * @param city
     * @return
     */
    public List<City> queryCityByProvince(City city) {
        db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM city WHERE province = ? AND area is null",
                new String[]{city.getProvinceName()});
        List<City> cities = new ArrayList<City>();
        while (cursor.moveToNext()) {
            City city1 = getCityInfo(cursor);
            if (!StringUtils.isEmpty(city1.getCityName())) {
                cities.add(city1);
            }
        }
        cursor.close();
        db.close();
        return cities;
    }

    /**
     * 通过省名称获取城市列表
     *
     * @param city
     * @return
     */
    public List<City> queryCityWhenProvinceLevel(City city) {
        db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM city WHERE province = ?", new String[]{city.getProvinceName()});
        List<City> cities = new ArrayList<City>();
        while (cursor.moveToNext()) {
            City city1 = getCityInfo(cursor);
            if (!StringUtils.isEmpty(city1.getAreasName())) {
                cities.add(city1);
            }
        }
        cursor.close();
        db.close();
        return cities;
    }

    /**
     * 通过省市获取三级区信息
     *
     * @param city
     * @param city
     * @return
     */
    public List<City> queryCityByCity(City city) {
        db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM city WHERE province = ? AND city = ?",
                new String[]{city.getProvinceName(), city.getCityName()});
        List<City> cities = new ArrayList<City>();
        while (cursor.moveToNext()) {
            City city1 = getCityInfo(cursor);
            if (!StringUtils.isEmpty(city1.getAreasName())) {
                cities.add(city1);
            }
        }
        cursor.close();
        db.close();
        return cities;
    }

    private City getCityInfo(Cursor cursor) {
        City city = new City();
        city.setProvinceName(cursor.getString(cursor.getColumnIndex("province")));
        boolean isPrivinceLevel = isProvinceLevelCity(cursor.getString(cursor.getColumnIndex("province")));
        if (isPrivinceLevel) {
            city.setCityName(cursor.getString(cursor.getColumnIndex("province")).toString());
        } else {
            city.setCityName(cursor.getString(cursor.getColumnIndex("city")));
        }

        if (isPrivinceLevel) {
            city.setAreasName(cursor.getString(cursor.getColumnIndex("city")));
        } else {
            city.setAreasName(cursor.getString(cursor.getColumnIndex("area")));
        }

        city.setCityCode(cursor.getInt(cursor.getColumnIndex("id")));

        return city;
    }

    /**
     * 判断是否是直辖市
     *
     * @param province
     * @return
     */
    private boolean isProvinceLevelCity(String province) {
        String citys = "香港,澳门,台湾,北京市,上海市,天津市,重庆市,";
        if (citys.contains(province + ",")) {
            return true;
        }
        return false;
    }
}
