package com.casic.dcms.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.luck.picture.lib.photoview.PhotoView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BigImageActivity extends AppCompatActivity {

    @BindView(R.id.imagePagerView)
    ViewPager imagePagerView;
    @BindView(R.id.pageNumberView)
    TextView pageNumberView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_image);
        ButterKnife.bind(this);

        initEvent();
    }

    @SuppressLint("SetTextI18n")
    private void initEvent() {
        int index = getIntent().getIntExtra("index", 0);
        ArrayList<String> urls = getIntent().getStringArrayListExtra("images");
        if (urls == null || urls.size() == 0) {
            return;
        }
        pageNumberView.setText("(" + (index + 1) + "/" + urls.size() + ")");
        pageNumberView.setTextColor(Color.RED);
        imagePagerView.setAdapter(new BigImageAdapter(this, urls));
        imagePagerView.setCurrentItem(index);
        imagePagerView.setOffscreenPageLimit(2);//设置预加载数量
        imagePagerView.setPageMargin(10);//控制两幅图之间的间距
        imagePagerView.setPageTransformer(true, new ZoomOutPageTransformer());
        imagePagerView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pageNumberView.setText("(" + (position + 1) + "/" + urls.size() + ")");
                pageNumberView.setTextColor(Color.RED);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    static class BigImageAdapter extends PagerAdapter {

        private Context context;
        private List<String> data;

        BigImageAdapter(@NotNull Context context, @NotNull List<String> imageList) {
            this.context = context;
            this.data = imageList;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public boolean isViewFromObject(@NotNull View view, @NotNull Object object) {
            return view == object;
        }

        @NotNull
        @Override
        public Object instantiateItem(@NotNull ViewGroup container, int position) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_big_picture, container, false);
            PhotoView photoView = view.findViewById(R.id.photoView);
            Glide.with(context).load(data.get(position)).into(photoView);
            photoView.setScaleType(ImageView.ScaleType.FIT_XY);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, @NotNull Object object) {
            container.removeView((View) object);
        }
    }

    //设置切换动画
    public static class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        //自由控制缩放比例
        private static final float MAX_SCALE = 1f;
        private static final float MIN_SCALE = 0.85f;//0.85f

        @Override
        public void transformPage(@NotNull View page, float position) {

            if (position <= 1) {
                float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);
                page.setScaleX(scaleFactor);
                if (position > 0) {
                    page.setTranslationX(-scaleFactor * 2);
                } else if (position < 0) {
                    page.setTranslationX(scaleFactor * 2);
                }
                page.setScaleY(scaleFactor);
            } else {
                page.setScaleX(MIN_SCALE);
                page.setScaleY(MIN_SCALE);
            }
        }
    }
}
