package com.base.browserwhite.ui.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.base.browserwhite.R;
import com.base.browserwhite.utils.BarUtils;

public class SearchActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.INSTANCE.setStatusBarLightMode(this, true);
        BarUtils.INSTANCE.setStatusBarColor(this, Color.TRANSPARENT);
        setContentView(R.layout.activity_search);

        ConstraintLayout layout = findViewById(R.id.appbar_layout);
        CoordinatorLayout.LayoutParams layoutParams =(CoordinatorLayout.LayoutParams) layout.getLayoutParams();
        int finalPosition = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
        layoutParams.topMargin = BarUtils.INSTANCE.getStatusBarHeight()+finalPosition;
        layout.setLayoutParams(layoutParams);
        layout.setVisibility(View.VISIBLE);
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void onBackPressed() {
        finish();
        setResult(10086);
        overridePendingTransition(0, 0);
    }


    public static void startActivityWithButtonAnimation(Activity currentActivity, View button,int verticalOffset) {
        int finalPosition = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, currentActivity.getResources().getDisplayMetrics());
        int buttonTop = button.getTop() - finalPosition;
        int distanceToTop = -buttonTop-verticalOffset;
        ObjectAnimator animator = ObjectAnimator.ofFloat(button, "translationY", button.getTranslationY(), distanceToTop);
        animator.setDuration(500);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                button.setVisibility(View.VISIBLE);
                Intent intent = new Intent(currentActivity, SearchActivity.class);
                currentActivity.startActivityForResult(intent, 10086);
                currentActivity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        animator.start();

    }
}
