package com.hainaos.vc.activity.preview;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hainaos.vc.R;
import com.hainaos.vc.adapter.PreviewAdapter;
import com.hainaos.vc.base.mvvm.BaseMvvmActivity;
import com.hainaos.vc.bean.PhotoInfo;
import com.hainaos.vc.databinding.ActivityPreviewBinding;
import com.hainaos.vc.view.ScaleCircleNavigator;

import net.lucode.hackware.magicindicator.ViewPagerHelper;

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

public class PreviewActivity extends BaseMvvmActivity<PreviewViewModel, ActivityPreviewBinding> {

    private PreviewAdapter mPreviewAdapter;
    private ScaleCircleNavigator scaleCircleNavigator;
    private ArrayList<PhotoInfo> mPhotoInfos;

    @Override
    public int getLayoutId() {
        return R.layout.activity_preview;
    }

    @Override
    public boolean setNightMode() {
        return false;
    }

    @Override
    protected void initDataBinding() {
        mViewModel.setCtx(this);
        mViewModel.setVDBinding(mViewDataBinding);
        mViewModel.setLifecycle(getLifecycleSubject());
//        mViewDataBinding.setClick(new BtnClick());
    }

    @Override
    public void initView() {
        mPreviewAdapter = new PreviewAdapter();
        mViewDataBinding.viewPager.setAdapter(mPreviewAdapter);
        scaleCircleNavigator = new ScaleCircleNavigator(this);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            finish();
            return;
        }
        ArrayList<PhotoInfo> photoInfos = bundle.getParcelableArrayList("mPhotoInfos");
        if (photoInfos == null) {
            finish();
            return;
        }
        int position = bundle.getInt("position");
        setImageViews(photoInfos);
        mViewDataBinding.viewPager.setCurrentItem(position);

    }

    private void setImageViews(List<PhotoInfo> photoInfos) {
        mViewDataBinding.magicIndicator.setNavigator(scaleCircleNavigator);
        scaleCircleNavigator.setCircleCount(photoInfos.size());
        scaleCircleNavigator.setNormalCircleColor(Color.DKGRAY);
        scaleCircleNavigator.setSelectedCircleColor(Color.LTGRAY);
        scaleCircleNavigator.setCircleClickListener(new ScaleCircleNavigator.OnCircleClickListener() {
            @Override
            public void onClick(int index) {

            }
        });
        ViewPagerHelper.bind(mViewDataBinding.magicIndicator, mViewDataBinding.viewPager);
        List<ImageView> imageViews = new ArrayList<>();
        for (PhotoInfo photoInfo : photoInfos) {
            ImageView image = new ImageView(this);
            image.setAdjustViewBounds(true);
            image.setScaleType(ImageView.ScaleType.FIT_CENTER);
            Glide.with(this).load(photoInfo.getFile()).into(image);
            imageViews.add(image);
        }
        mPreviewAdapter.setImageViews(imageViews);
        mPreviewAdapter.notifyDataSetChanged();
    }


}
