package com.secspace.lib.common.utils;

import android.content.Context;
import android.telephony.SubscriptionManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectSubscriptionUtil {

    public void setDefaultDataSubId(Context context, int subId) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            try {
                Method method = subscriptionManager.getClass().getMethod("setDefaultDataSubId", int.class);
                if (method != null) {
                    method.invoke(subscriptionManager, subId);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                try {
                    Method method = subscriptionManager.getClass().getMethod("setDefaultDataSubscriptionId", int.class);
                    if (method != null) {
                        method.invoke(subscriptionManager, subId);
                    }
                } catch (NoSuchMethodException e1) {
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                    e1.printStackTrace();
                }
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public static int getDefaultDataSubId(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = SubscriptionManager.from(context.getApplicationContext());
            try {
                Method getSubId = subscriptionManager.getClass().getMethod("getDefaultDataSubId");
                if (getSubId != null) {
                    return (int) getSubId.invoke(subscriptionManager);
                }
            } catch (NoSuchMethodException e) {
                try {
                    Method getSubId = subscriptionManager.getClass().getMethod("getDefaultDataSubscriptionId");
                    if (getSubId != null) {
                        return (int) getSubId.invoke(subscriptionManager);
                    }
                } catch (NoSuchMethodException e1) {
                    e.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e.printStackTrace();
                } catch (InvocationTargetException e1) {
                    e.printStackTrace();
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }


    public int getSlotIndex(Context context, int subId) {
        int slotIndex = -1;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            try {
                Method method = subscriptionManager.getClass().getMethod("getSlotIndex", int.class);
                if (method != null) {
                    slotIndex = (int) method.invoke(subscriptionManager, subId);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return slotIndex;
    }


    public int[] getSubId(Context context, int slotIndex) {
        int[] subIds = new int[]{};
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            try {
                Method method = subscriptionManager.getClass().getMethod("getSubId", int.class);
                if (method != null) {
                    subIds = (int[]) method.invoke(subscriptionManager, slotIndex);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return subIds;
    }

    public int getDefaultVoiceSubscriptionId(Context context) {
        int subId = -1;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            try {
                Method method = subscriptionManager.getClass().getMethod("getDefaultVoiceSubscriptionId", new Class[]{});
                if (method != null) {
                    subId = (int) method.invoke(subscriptionManager, new Object[]{});
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return subId;
    }

    public void setDefaultVoiceSubId(Context context, int subId) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            try {
                Method method = subscriptionManager.getClass().getMethod("setDefaultVoiceSubId", int.class);
                if (method != null) {
                    method.invoke(subscriptionManager, subId);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    public int getDefaultSmsSubscriptionId(Context context) {
        int subId = -1;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            try {
                Method method = subscriptionManager.getClass().getMethod("getDefaultSmsSubscriptionId", new Class[]{});
                if (method != null) {
                    subId = (int) method.invoke(subscriptionManager, new Object[]{});
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return subId;
    }

    public static void setDefaultSmsSubId(Context context, int subId) {

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            try {
                Method method = subscriptionManager.getClass().getMethod("setDefaultSmsSubId", int.class);
                if (method != null) {
                    method.invoke(subscriptionManager, subId);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
