package com.bassbooster.soundeffects.equalizer.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Looper;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bassbooster.equalizer.R;

public class AppUtils {

    public static boolean haveNetworkConnection(Context context) {
        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;

    }

    public static void setMargins(View view, int left, int top, int right, int bottom) {
        if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            p.setMargins(left, top, right, bottom);
            view.requestLayout();
        }
    }

    public static void setGradientTextView(TextView textView) {
        Context context = textView.getContext();
        TextPaint paint = textView.getPaint();
        float width = paint.measureText(textView.getText().toString());

        Shader textShader = new LinearGradient(0, 0, width, textView.getTextSize(), new int[]{
//                ContextCompat.getColor(context, R.color.clr_05F1FF),
//                ContextCompat.getColor(context, R.color.clr_005BE0)
        },
                null, Shader.TileMode.CLAMP);
        textView.getPaint().setShader(textShader);
    }

    public static void hideNavigationBar(AppCompatActivity activity) {
        int flags =  View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        if (activity != null){
            View decorView = activity.getWindow().getDecorView();
            Handler handler = new Handler(Looper.getMainLooper());
            handler.removeCallbacksAndMessages(null);
            handler.postDelayed(()->decorView.setSystemUiVisibility(flags),1000);
        }
    }

    public static AppInfo getAppInfo(String packageName, Resources resources){
        AppInfo app;
        switch (packageName) {
            case "com.gaana" :
                app = new AppInfo("Gaana", "com.gaana.SplashScreenActivity",
                        BitmapFactory.decodeResource(resources,R.drawable.ic_gaana));
                app.setPackageName(packageName);
                return app;
            case "com.spotify.music" :
                app = new AppInfo("Spotify", "com.spotify.music.MainActivity",
                        BitmapFactory.decodeResource(resources,R.drawable.ic_spotify));
                return app;
            case "com.soundcloud.android" :
                app = new AppInfo("Soundcloud", "com.soundcloud.android.launcher.LauncherActivity",
                        BitmapFactory.decodeResource(resources,R.drawable.ic_soundcloud));
                return app;
            case "com.google.android.apps.youtube.music" :
                app = new AppInfo("Youtube Music", "com.google.android.apps.youtube.music.activities.MusicActivity",
                        BitmapFactory.decodeResource(resources,R.drawable.ic_ytbmusic));
                return app;
            default: return new AppInfo("","",null);

                //"com.soundcloud.android.playback.players.MediaService"
        }
    }

}
