package com.xplora.xpsettings;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.TelephonyManager;

import com.xplora.xpsettings.Model.ApnModel;

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

public class ApnManager {
    public static Uri APN_URI = Uri.parse("content://telephony/carriers");
    public static Uri CURRENT_APN_URI = Uri.parse("content://telephony/carriers/preferapn");

    @SuppressLint("Range")
    public static List<ApnModel> getAPNList(Context context){
        List<ApnModel> list = new ArrayList<ApnModel>();
        ContentResolver resolver = context.getContentResolver();

        Cursor cr = resolver.query(APN_URI, null, null, null, null);
        while(cr!=null && cr.moveToNext()){
            ApnModel a = new ApnModel();
            a.apn_id = cr.getString(cr.getColumnIndex("_id"));
            a.apn_name = cr.getString(cr.getColumnIndex("apn"));
            a.user_name = cr.getString(cr.getColumnIndex("user"));
            a.password = cr.getString(cr.getColumnIndex("password"));
            a.mcc = cr.getString(cr.getColumnIndex("mcc"));
            a.mnc = cr.getString(cr.getColumnIndex("mnc"));
            list.add(a);
        }
        if(cr!=null)
            cr.close();
        return list;
    }

    public static void addAPN(Context context, String apn_name, String user_name, String password, String mcc, String mnc) {
        int id = -1;
        String NUMERIC = getSIMInfo(context);
        if (NUMERIC == null) {
            return;
        }
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("name", apn_name);                                  //apn中文描述
        values.put("apn", apn_name);                                     //apn名称
        values.put("type", "default");                            //apn类型
        values.put("numeric", NUMERIC);
        values.put("mcc", mcc);
        values.put("mnc", mnc);
        values.put("proxy", "");                                        //代理
        values.put("port", "");                                         //端口
        values.put("mmsproxy", "");                                     //彩信代理
        values.put("mmsport", "");                                      //彩信端口
        values.put("user", user_name);                                         //用户名
        values.put("server", "");                                       //服务器
        values.put("password", password);                                     //密码
        values.put("mmsc", "");                                          //MMSC
        Cursor c = null;
        Uri newRow = resolver.insert(APN_URI, values);
        if (newRow != null) {
            c = resolver.query(newRow, null, null, null, null);
            int idIndex = c.getColumnIndex("_id");
            c.moveToFirst();
            id = c.getShort(idIndex);
        }
        if (c != null)
            c.close();
    }

    // 设置接入点
    public static void setAPN(Context context, int id) {
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("apn_id", id);
        resolver.update(CURRENT_APN_URI, values, null, null);
    }

    public static String getSIMInfo(Context context) {
        TelephonyManager iPhoneManager = (TelephonyManager)context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return iPhoneManager.getSimOperator();
    }
}