package com.base.browserwhite.label;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.base.browserwhite.R;
import com.base.browserwhite.label.fragment.RedFragment;

public class ViewPager2Manager {
    public ViewPager2 mViewPager ;

    public FragmentActivity context;
    private ViewPagerAdapter viewPagerAdapter;

    public ViewPager2Manager(ViewPager2 viewpager, FragmentActivity context) {
        this.mViewPager = viewpager;
        this.context = context;
        initViewPager();
    }

    private void initViewPager(){
        viewPagerAdapter = new ViewPagerAdapter(context);


        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback());
        itemTouchHelper.attachToRecyclerView((RecyclerView) mViewPager.getChildAt(0));
        mViewPager.setPageTransformer(new LabelPageTransformer());

        mViewPager.setAdapter(viewPagerAdapter);

        Fragment newFragment = new RedFragment();
        viewPagerAdapter.addFragment(newFragment);


        Fragment newFragment1 = new RedFragment();
        viewPagerAdapter.addFragment(newFragment1);
        mViewPager.setCurrentItem(viewPagerAdapter.getItemCount(),false);

    }

    private boolean isScaled = false; // 标记是否执行了缩放
    public void manageFragments() {
        isScaled = true;
        for (int i = 0; i < viewPagerAdapter.getItemCount(); i++) {
            Fragment fragment = viewPagerAdapter.getItem(i);
            View fragmentView = ((RedFragment) fragment).getCachedView();
            if (fragmentView != null) {
                applyScaleEffect(fragmentView, true);

            }
        }
        mViewPager.setUserInputEnabled(true);
    }

    public void resetFragments() {
        isScaled = false;
        for (int i = 0; i < viewPagerAdapter.getItemCount(); i++) {
            Fragment fragment = viewPagerAdapter.getItem(i);
            View fragmentView = ((RedFragment) fragment).getCachedView();
            if (fragmentView != null) {
                applyScaleEffect(fragmentView, false);
            }
        }
        mViewPager.setUserInputEnabled(false); // 禁止滑动
    }

    public void addFragment(){
        Fragment newFragment = new RedFragment();
        viewPagerAdapter.addFragment(newFragment);
        mViewPager.setOffscreenPageLimit(viewPagerAdapter.getItemCount());
        mViewPager.setCurrentItem(viewPagerAdapter.getItemCount() - 1, false);
        if (isScaled) {
            View fragmentView = ((RedFragment) newFragment).getCachedView();
            if (fragmentView != null) {
                applyScaleEffect(fragmentView, false);
            }
            mViewPager.setUserInputEnabled(false);
            isScaled = false;
        }
    }
    private void applyScaleEffect(View fragmentView, boolean scale) {
        if (fragmentView != null) {
            float targetScale = scale ? 0.8f : 1f; // 缩放目标值
            ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(fragmentView, "scaleX", targetScale);
            ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(fragmentView, "scaleY", targetScale);
            float viewWidth = fragmentView.getWidth();
            float viewHeight = fragmentView.getHeight();
            fragmentView.setPivotX(viewWidth / 2f);
            fragmentView.setPivotY(viewHeight / 2f);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(scaleXAnimator, scaleYAnimator);
            animatorSet.setDuration(500); // 动画持续时间
            animatorSet.start();
        }
    }

    private class SwipeToDeleteCallback extends ItemTouchHelper.SimpleCallback {

        public SwipeToDeleteCallback() {
            super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
        }

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            viewPagerAdapter.removeFragment(viewPagerAdapter.getItem(position));
        }
    }
}
