package com.xplora.xpsettings.Data;

import android.Manifest;
import android.app.ActivityManagerNative;
import android.app.Application;
import android.app.IActivityManager;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.LocaleList;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.core.app.ActivityCompat;

import com.xplora.xpsettings.Model.ApnModel;
import com.xplora.xpsettings.Utils.Constant;

import java.lang.reflect.Field;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;

public class DataManager extends Application {
    private static Context sContext;
    public static final String socketAction = "com.xplora.localsocket";
    public static final short ACTION_SCREEN_TIME_CHANGED = 0x01;
    public static final short ACTION_BRIGHTNESS_CHANGED = 0x02;

    public static int mDataRoamingType = 0;
    public static int mBrightness = 50;
    public static int mScreenTime = 60;
    public static int mRingTone = 1;
    public static int mRingerMode = 1;
    public static int mSystemVolume = 10;
    public static int mMediaVolume = 50;
    public static int mCallVolume = 80;
    public static int mIsVibrate = 1;
    public static int mAppNameType = 0;
    public static String mPhotoResolution;
    public static String mVideoResolution;


    @Override
    public void onCreate() {
        super.onCreate();
        sContext = getApplicationContext();
        loadSetting();
    }

    public static void sendLocalSocketIntent(short type, int lastValue) {
        Intent intent = new Intent(socketAction);
        intent.putExtra("type", type);
        intent.putExtra("value", lastValue);
        ComponentName componentName = new ComponentName("com.xplora.commonservice",
                "com.xplora.commonservice.modules.receiver.LocalSocketReceiver");
        intent.setComponent(componentName);
        sContext.sendBroadcast(intent, "com.xplora.permission.localsocket");
    }

    public void loadSetting() {
        List dataList = DatabaseHelper.querySetting(sContext);
        if (dataList.size() >= 3) {
            setAppNameType((int) dataList.get(0));
            setPhotoResolution((String) dataList.get(1));
            setVideoResolution((String) dataList.get(2));
        }
    }

    public static int getBrightness() {
//        if (Constant.DEBUG) {
//            return mBrightness;
//        }

        int value = Settings.System.getInt(sContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 125);
        return value * 100 / 255;
    }

    public static void setBrightness(int value) {
//        if (Constant.DEBUG) {
//            mBrightness = value;
//            return;
//        }

        Settings.System.putInt(sContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, value * 255 / 100);
    }

    public static int getAutoBrightness() {
        //Settings.System.getFloat(sContext.getContentResolver(), Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.5f);
        return 0;
    }

    public static int getScreenTime() {
//        if (Constant.DEBUG) {
//            return mScreenTime;
//        }

        AtomicInteger time = new AtomicInteger(10 * 1000);
        return Settings.System.getInt(sContext.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, time.intValue());
    }

    public static void setScreenTime(int value) {
//        if (Constant.DEBUG) {
//            mScreenTime = value;
//            return;
//        }

        Settings.System.putInt(sContext.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, value);
    }

    public static int getRingTone() {
        if (Constant.DEBUG) {
            return mRingTone;
        } else {
//            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
//            return RingtoneManager.getRingtone(sContext, notification);
            return mRingTone;
        }
    }

    public static void setRingTone(int value) {
        if (Constant.DEBUG) {
            mRingTone = value;
        } else {
            mRingTone = value;
        }
    }

    public static int getRingerMode(Context context) {
//        public static final int RINGER_MODE_SILENT = 0;
//        public static final int RINGER_MODE_VIBRATE = 1;
//        public static final int RINGER_MODE_NORMAL = 2;
        if (Constant.DEBUG) {
            return mRingerMode;
        } else {
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            return audioManager.getRingerMode();
        }
    }

    public static void setRingerMode(int value) {
//        public static final int RINGER_MODE_SILENT = 0;
//        public static final int RINGER_MODE_VIBRATE = 1;
//        public static final int RINGER_MODE_NORMAL = 2;
        if (Constant.DEBUG) {
            mRingerMode = value;
        } else {
            AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setRingerMode(value);
        }
    }

    //获取是否允许静音
    public static boolean hasSilentPermission() {
        NotificationManager notificationManager = (NotificationManager)sContext.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !notificationManager.isNotificationPolicyAccessGranted()) {
            return false;
        }
        return true;
    }

    public static int getSystemVolume() {
        if (Constant.DEBUG) {
            return mSystemVolume;
        } else {
            AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
            return audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
        }
    }

    public static void setSystemVolume(int value) {
        if (Constant.DEBUG) {
            mSystemVolume = value;
        } else {
            AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, value, AudioManager.FLAG_ALLOW_RINGER_MODES);
        }
    }

    public static int getMediaVolume() {
        if (Constant.DEBUG) {
            return mMediaVolume;
        } else {
            AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
            return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        }
    }

    public static void setMediaVolume(int value) {
        if (Constant.DEBUG) {
            mMediaVolume = value;
        } else {
            AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, AudioManager.FLAG_ALLOW_RINGER_MODES);
        }
    }

    public static int getCallVolume() {
        if (Constant.DEBUG) {
            return mCallVolume;
        } else {
            AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
            return audioManager.getStreamVolume(AudioManager.STREAM_RING);
        }
    }

    public static void setCallVolume(int value) {
        if (Constant.DEBUG) {
            mCallVolume = value;
        } else {
            AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
            audioManager.setStreamVolume(AudioManager.STREAM_RING, value, AudioManager.FLAG_ALLOW_RINGER_MODES);
        }
    }

    public static int getIsVibrate() {
        return mIsVibrate;
    }

    public static void setIsVibrate(int mIsVibrate) {
        DataManager.mIsVibrate = mIsVibrate;
        AudioManager audioManager = (AudioManager)sContext.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, mIsVibrate);
    }

    public static int getVolume(int type) {
        if (type == 1) {
            return DataManager.getMediaVolume();
        } else if (type == 2) {
            return DataManager.getCallVolume();
        } else {
            return DataManager.getSystemVolume();
        }
    }

    public static void setVolume(int type, int value) {
        if (type == 1) {
            DataManager.setMediaVolume(value);
        } else if (type == 2) {
            DataManager.setCallVolume(value);
        } else {
            DataManager.setSystemVolume(value);
        }
    }

    public static String getLanguage(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        return locale.getLanguage();
    }

    public static int getDataRoamingType() {
        if (Constant.DEBUG) {
            return mDataRoamingType;
        } else {
            TelephonyManager telephonyManager = (TelephonyManager)sContext.getSystemService(Context.TELEPHONY_SERVICE);
            return telephonyManager.isDataRoamingEnabled() ? 1 : 0;
        }
    }

    public static void setDataRoamingType(int value) {
        if (Constant.DEBUG) {
            mDataRoamingType = value;
        } else {
            TelephonyManager telephonyManager = (TelephonyManager)sContext.getSystemService(Context.TELEPHONY_SERVICE);
            telephonyManager.setDataEnabled(value == 1);
        }
    }

    public static int getAppNameType() {
        if (Constant.DEBUG) {
            return mAppNameType;
        } else {
            //return SystemProperties.get("");
            return mAppNameType;
        }
    }

    public static void setAppNameType(int value) {
        if (Constant.DEBUG) {
            mAppNameType = value;
        } else {
            DatabaseHelper.update(sContext, MetaData.KEY_APPNAME, String.valueOf(value));
        }
    }

    public static String getPhotoResolution() {
        if (Constant.DEBUG) {
            return mPhotoResolution;
        } else {
            return mPhotoResolution;
        }
    }

    public static void setPhotoResolution(String value) {
        if (Constant.DEBUG) {
            mPhotoResolution = value;
        } else {
            DatabaseHelper.update(sContext, MetaData.KEY_RESOLUTION_PHOTO, value);
        }
    }

    public static String getVideoResolution() {
        if (Constant.DEBUG) {
            return mVideoResolution;
        } else {
            return mVideoResolution;
        }
    }

    public static void setVideoResolution(String value) {
        if (Constant.DEBUG) {
            mVideoResolution = value;
        } else {
            DatabaseHelper.update(sContext, MetaData.KEY_RESOLUTION_VIDEO, value);
        }
    }
}
