package it.integry.integrywmsnative.ui;

import android.animation.Animator;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.os.Build;
import androidx.core.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewPropertyAnimator;
import android.view.Window;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.fede987.statusbaralert.utils.ScreenUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import kotlin.TypeCastException;
import kotlin.jvm.internal.Intrinsics;

public class StatusBarAlert {
    
        private static final Map allAlerts = (Map)(new LinkedHashMap());
        private static int statusBarColorOringinal;
        private static boolean hasOriginalStatusBarTranslucent;
        public static final StatusBarAlert.Companion Companion = new StatusBarAlert.Companion();

        
        public static final class Builder {
            private int text;
            private String stringText;
            private int alertColor;
            private boolean showProgress;
            private long duration;
            private boolean autoHide;
            private Activity context;

            @Nullable
            public final View build() {
                return StatusBarAlert.Companion.addStatusBarTextAndProgress(this.context, this.text, this.stringText, this.alertColor, this.showProgress, this.autoHide, this.duration);
            }

            @NotNull
            public final StatusBarAlert.Builder withAlertColor(int alertColor) {
                this.alertColor = alertColor;
                return this;
            }

            @NotNull
            public final StatusBarAlert.Builder withText(int text) {
                this.text = text;
                return this;
            }

            @NotNull
            public final StatusBarAlert.Builder withText(@NotNull String text) {
                Intrinsics.checkParameterIsNotNull(text, "text");
                this.stringText = text;
                return this;
            }

            @NotNull
            public final StatusBarAlert.Builder showProgress(boolean showProgress) {
                this.showProgress = showProgress;
                return this;
            }

            @NotNull
            public final StatusBarAlert.Builder autoHide(boolean autoHide) {
                this.autoHide = autoHide;
                return this;
            }

            @NotNull
            public final StatusBarAlert.Builder withDuration(long millis) {
                this.duration = millis;
                return this;
            }

            public Builder(@NotNull Activity context) {
                super();
                Intrinsics.checkParameterIsNotNull(context, "context");
                this.context = context;
                this.stringText = "";
                this.duration = 2000L;
                this.autoHide = true;
            }
        }

        public static final class Companion {
            @Nullable
            public final View addStatusBarTextAndProgress(@NotNull final Activity any, @Nullable Integer text, @Nullable String stringText, int alertColor, boolean showProgress, boolean autoHide, long duration) {

                this.hide(any, null);
                Window var10000 = any.getWindow();

                View var18 = var10000.getDecorView();
                if (var18 == null) {
                    throw new TypeCastException("null cannot be cast to non-null type android.view.ViewGroup");
                } else {

                    ViewGroup decor = (ViewGroup)var18;
                    final LinearLayout statusBarAlert = new LinearLayout(any);
                    statusBarAlert.setLayoutParams(new ViewGroup.LayoutParams(-1, ScreenUtils.Companion.getStatusBarHeight(any)));
                    statusBarAlert.setGravity(1);

                    if (alertColor > 0) {
                        statusBarAlert.setBackgroundColor(ContextCompat.getColor(any, alertColor));
                    }

                    LinearLayout ll2;
                    TextView t;
                    CharSequence var19;
                    label58: {
                        ll2 = new LinearLayout(any);
                        ll2.setOrientation(LinearLayout.HORIZONTAL);
                        ll2.setGravity(16);
                        ll2.setLayoutParams(new ViewGroup.LayoutParams(-2, ScreenUtils.Companion.getStatusBarHeight(any)));
                        t = new TextView(any);
                        t.setLayoutParams(new ViewGroup.LayoutParams(-2, ScreenUtils.Companion.getStatusBarHeight(any)));
                        t.setTextSize(12.0F);
                        t.setTextColor(-1);
                        t.setGravity(17);
                        if (text != null) {
                            if (text == 0) {
                                var19 = (Intrinsics.areEqual(stringText, "") ^ true ? "" + stringText + ' ' : "");
                                break label58;
                            }
                        }

                        StringBuilder var10001 = new StringBuilder();
                        Resources var10002 = any.getResources();
                        if (text == null) {
                            Intrinsics.throwNpe();
                        }

                        var19 = var10001.append(var10002.getString(text)).append(" ").toString();
                    }

                    t.setText(var19);
                    t.setIncludeFontPadding(false);
                    ll2.addView(t);
                    if (showProgress) {
                        ProgressBar p = new ProgressBar(any);
                        p.setIndeterminate(true);
                        p.getIndeterminateDrawable().setColorFilter(-1, PorterDuff.Mode.SRC_IN);
                        p.setLayoutParams(new ViewGroup.LayoutParams(ScreenUtils.Companion.convertDpToPixel(11.0F, any), ScreenUtils.Companion.convertDpToPixel(11.0F, (Context)any)));
                        ll2.addView(p);
                    }

                    statusBarAlert.addView(ll2);
                    var10000 = any.getWindow();
                    Intrinsics.checkExpressionValueIsNotNull(var10000, "any.window");
                    var18 = var10000.getDecorView();
                    Intrinsics.checkExpressionValueIsNotNull(var18, "any.window.decorView");
                    View decorView = var18.getRootView();
                    Intrinsics.checkExpressionValueIsNotNull(decorView, "decorView");
                    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                    StatusBarAlert.hasOriginalStatusBarTranslucent = this.isTranslucentStatusBar(any);
                    if (Build.VERSION.SDK_INT >= 21) {
                        any.getWindow().clearFlags(67108864);
                        var10000 = any.getWindow();
                        Intrinsics.checkExpressionValueIsNotNull(var10000, "any.window");
                        StatusBarAlert.statusBarColorOringinal = var10000.getStatusBarColor();
                        var10000 = any.getWindow();
                        Intrinsics.checkExpressionValueIsNotNull(var10000, "any.window");
                        var10000.setStatusBarColor(0);
                    }

                    decor.addView(statusBarAlert);
                    ll2.setTranslationY(-((float)ScreenUtils.Companion.convertDpToPixel(25.0F, any)));
                    ViewPropertyAnimator var20 = ll2.animate();
                    if (var20 == null) {
                        Intrinsics.throwNpe();
                    }

                    var20.translationY(0.0F).setDuration(150L).setStartDelay(350L).setInterpolator((new AccelerateDecelerateInterpolator())).start();
                    if (autoHide) {
                        statusBarAlert.postDelayed((new Runnable() {
                            public final void run() {
                                if (!any.isFinishing() && statusBarAlert.getParent() != null) {
                                    StatusBarAlert.Companion.hideInternal(any, statusBarAlert, null);
                                }

                                Map var10000 = StatusBarAlert.allAlerts;
                                ComponentName var10001 = any.getComponentName();
                                Intrinsics.checkExpressionValueIsNotNull(var10001, "any.componentName");
                                var10000.remove(var10001.getClassName());
                            }
                        }), duration + (long)500);
                    }

                    Map var21 = StatusBarAlert.allAlerts;
                    ComponentName var22 = any.getComponentName();
                    Intrinsics.checkExpressionValueIsNotNull(var22, "any.componentName");
                    if (var21.get(var22.getClassName()) == null) {
                        Map var14 = StatusBarAlert.allAlerts;
                        ComponentName var23 = any.getComponentName();
                        Intrinsics.checkExpressionValueIsNotNull(var23, "any.componentName");
                        String var24 = var23.getClassName();
                        Intrinsics.checkExpressionValueIsNotNull(var24, "any.componentName.className");
                        String var15 = var24;
                        List var16 = new ArrayList();
                        var14.put(var15, var16);
                    }

                    var21 = StatusBarAlert.allAlerts;
                    var22 = any.getComponentName();
                    Intrinsics.checkExpressionValueIsNotNull(var22, "any.componentName");
                    List var25 = (List)var21.get(var22.getClassName());
                    if (var25 != null) {
                        var25.add(statusBarAlert);
                    }

                    return statusBarAlert;
                }
            }

            public final void hide(@NotNull Activity any, @Nullable Runnable onHidden) {
                Map var10000;
                ComponentName var10001;
                List var9;
                label48: {
                    var10000 = StatusBarAlert.allAlerts;
                    var10001 = any.getComponentName();

                    if (var10000.get(var10001.getClassName()) != null) {
                        var10000 = StatusBarAlert.allAlerts;
                        var10001 = any.getComponentName();

                        var9 = (List)var10000.get(var10001.getClassName());
                        if (var9 == null) {
                            break label48;
                        }

                        if (var9.size() != 0) {
                            break label48;
                        }
                    }

                    if (onHidden != null) {
                        onHidden.run();
                    }

                    return;
                }

                var10000 = StatusBarAlert.allAlerts;
                var10001 = any.getComponentName();
                var9 = (List)var10000.get(var10001.getClassName());
                if (var9 != null) {
                    Iterable $receiver$iv = var9;
                    Iterator var4 = $receiver$iv.iterator();

                    while(var4.hasNext()) {
                        Object element$iv = var4.next();
                        LinearLayout it = (LinearLayout)element$iv;
                        StatusBarAlert.Companion.hideInternal(any, it, onHidden);
                    }
                }

                var10000 = StatusBarAlert.allAlerts;
                var10001 = any.getComponentName();
                var9 = (List)var10000.get(var10001.getClassName());
                if (var9 != null) {
                    var9.clear();
                }

            }

            private final void hideInternal(Activity any, final LinearLayout it, final Runnable onHidden) {
                if (it.getParent() != null) {
                    Window var10000 = any.getWindow();
                    View var5 = var10000.getDecorView();
                    var5 = var5.getRootView();
                    var5.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

                    if (Build.VERSION.SDK_INT >= 21) {
                        var10000 = any.getWindow();
                        var10000.setStatusBarColor(StatusBarAlert.statusBarColorOringinal);
                        if (StatusBarAlert.hasOriginalStatusBarTranslucent) {
                            any.getWindow().addFlags(67108864);
                        }
                    }

                    var10000 = any.getWindow();
                    var5 = var10000.getDecorView();
                    if (var5 == null) {
                        throw new TypeCastException("null cannot be cast to non-null type android.view.ViewGroup");
                    }

                    final ViewGroup decor = (ViewGroup)var5;
                    ViewPropertyAnimator var6 = it.animate();
                    if (var6 != null) {
                        var6 = var6.translationY(-((float)ScreenUtils.Companion.convertDpToPixel(25.0F, (Context)any)));
                        if (var6 != null) {
                            var6 = var6.setDuration(150L);
                            if (var6 != null) {
                                var6 = var6.setStartDelay(500L);
                                if (var6 != null) {
                                    var6 = var6.setInterpolator(new AccelerateInterpolator());
                                    if (var6 != null) {
                                        var6 = var6.setListener((new Animator.AnimatorListener() {
                                            public void onAnimationRepeat(@Nullable Animator animation) {
                                            }

                                            public void onAnimationEnd(@Nullable Animator animation) {
                                                decor.removeView((View)it);
                                                Runnable var10000 = onHidden;
                                                if (onHidden != null) {
                                                    var10000.run();
                                                }

                                            }

                                            public void onAnimationStart(@Nullable Animator animation) {
                                            }

                                            public void onAnimationCancel(@Nullable Animator animation) {
                                            }
                                        }));
                                        if (var6 != null) {
                                            var6.start();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }

            private final boolean isTranslucentStatusBar(Context any) {
                if (any == null) {
                    throw new TypeCastException("null cannot be cast to non-null type android.app.Activity");
                } else {
                    Window w = ((Activity)any).getWindow();
                    android.view.WindowManager.LayoutParams lp = w.getAttributes();
                    int flags = lp.flags;
                    if (Build.VERSION.SDK_INT >= 19) {
                        return (flags & 67108864) == 67108864;
                    } else {
                        return false;
                    }
                }
            }

            private Companion() {
            }

        }
    }
    
