package com.sjkj.appthreefloor_tsgz.weight.photoview;

import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.databinding.ActivityPhotoViewBinding;
import com.sjkj.base_lib.view.BaseActivity;

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

/**
 * 查看大图
 */
public class PhotoViewActivity extends BaseActivity {
    private ActivityPhotoViewBinding binding;
    private  PhotoViewModel viewModel;
    public static final String TAG = PhotoViewActivity.class.getSimpleName();
    private int currentPosition;
    private MyImageAdapter adapter;
    private List<String> imageUrls;

    @Override
    protected int initLayout() {
        return R.layout.activity_photo_view;
    }


    @Override
    public void initBinding() {
        super.initBinding();
        binding = (ActivityPhotoViewBinding) getViewDataBinding();
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(PhotoViewModel.class);
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);
    }

    @Override
    public void initView() {
        super.initView();
        imageUrls = (ArrayList<String>) getIntent().getStringArrayListExtra("positionDataList");
        if (imageUrls != null) {
            currentPosition = getIntent().getIntExtra("position", 0);
            adapter = new MyImageAdapter(imageUrls, this);
            binding.viewPager.setAdapter(adapter);
            binding.viewPager.setCurrentItem(currentPosition, false);
            viewModel.title.set(currentPosition + 1 + "/" + imageUrls.size());
            binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    currentPosition = position;
                    viewModel.title.set(currentPosition + 1 + "/" + imageUrls.size());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void loadData() {
        super.loadData();
    }
}
