package com.base.browserwhite.ui.activity.search;

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.os.Handler;
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.help.SearchTipUtils;
import com.base.browserwhite.utils.AppPreferences;
import com.base.browserwhite.utils.BarUtils;
import com.base.browserwhite.utils.LogEx;
import com.base.browserwhite.utils.ToastUtils;

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);

        new Thread(new Runnable() {
            @Override
            public void run() {
                String a = SearchTipUtils.INSTANCE.getSearchTip("w");
                new Handler(getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (a != null) {
                            ToastUtils.INSTANCE.toast(getApplicationContext(), a);
                        } else {
                            ToastUtils.INSTANCE.toast(getApplicationContext(), "空的");
                        }
                    }
                });

            }
        }
        ).start();
        String json = AppPreferences.getInstance().getString("SearchTipUtils", "");
        LogEx.INSTANCE.logDebug("SearchTipUtils", "json=" + json, false);
    }

    @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();

    }
}
