package com.casic.dcms.view;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityBigImageBinding;
import com.casic.dcms.utils.ViewGroupKit;
import com.luck.picture.lib.photoview.PhotoView;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.utils.Constant;

import java.util.ArrayList;

public class BigImageActivity extends AndroidxBaseActivity<ActivityBigImageBinding> {

    @Override
    public void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, false, R.color.black);
        binding.leftBackView.setOnClickListener(view -> finish());
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    public void initOnCreate(@Nullable Bundle savedInstanceState) {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void initEvent() {
        int index = getIntent().getIntExtra(Constant.BIG_IMAGE_INTENT_INDEX_KEY, 0);
        ArrayList<String> urls = getIntent().getStringArrayListExtra(Constant.BIG_IMAGE_INTENT_DATA_KEY);
        if (urls == null || urls.size() == 0) {
            return;
        }
        binding.pageNumberView.setText("(" + (index + 1) + "/" + urls.size() + ")");
        binding.imagePagerView.setAdapter(new BigImageAdapter(this, urls));
        binding.imagePagerView.setCurrentItem(index);
        binding.imagePagerView.setOffscreenPageLimit(2);
        binding.imagePagerView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

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

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class BigImageAdapter extends PagerAdapter {

        private final Context context;
        private final ArrayList<String> data;

        public BigImageAdapter(Context context, ArrayList<String> data) {
            this.context = context;
            this.data = data;
        }

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

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

        @NonNull
        @Override
        public Object instantiateItem(@NonNull 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.CENTER_INSIDE);
            container.addView(view);
            //点击大图取消预览
            photoView.setOnClickListener(v -> finish());
            return view;
        }

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