package com.common.im.ui.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Parcelable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.alibaba.android.arouter.facade.annotation.Route;
import com.common.im.contract.ImAppealContract;
import com.common.im.contract.PhotoPreviewContract;
import com.common.im.presenter.ImAppealPresenter;
import com.common.im.presenter.PhotoPreviewPresenter;
import com.common.im_ui.R;
import com.common.im_ui.databinding.ActivityImAppealBinding;
import com.common.im_ui.databinding.ActivityPhotoPreviewBinding;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.GlideUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.photoview.PhotoView;

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

/**
 * 创建日期：2022/6/13 14:30
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.ChatCenter.CHAT_PHOTO_PREVIEW)
public class PhotoPreviewActivity extends BaseMVPActivity<ActivityPhotoPreviewBinding, PhotoPreviewPresenter> implements PhotoPreviewContract.PhotoPreviewView, View.OnClickListener {
    @Override
    public void onClick(View v) {

    }
    private TextView tv_title;
    ViewPager viewpager;
    private boolean isRemove;
    private List<LocalMedia> selectList;
    private int selectedPosition = 0;
    private PhotoPreviewListAdapter adapter;

    @Override
    protected void initView() {
        selectList = getIntent().getParcelableArrayListExtra("list");
        selectedPosition = getIntent().getIntExtra("position", 0);
        isRemove = getIntent().getBooleanExtra("isRemove", true);
        if (selectList == null) {
            selectList = new ArrayList<>();
            selectedPosition = 0;
        }
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "");
        tv_title= viewBinding.toolbarInclude.title;
        tv_title.setText((selectedPosition + 1) + "/" + selectList.size());
        adapter = new PhotoPreviewListAdapter();
        viewpager=viewBinding.viewpager;
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(selectedPosition);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedPosition = position;
                tv_title.setText((position + 1) + "/" + selectList.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        if (isRemove) {
            viewBinding.toolbarInclude.tvRight.setVisibility(View.VISIBLE);
            viewBinding.toolbarInclude.tvRight.setBackgroundResource(com.cooleshow.base.R.drawable.ic_delete);
            viewBinding.toolbarInclude.tvRight.setOnClickListener(view -> {
                if (selectList.size() > 0) {
                    if (selectedPosition > selectList.size() - 1) {
                        selectedPosition -= 1;
                    }
                    selectList.remove(selectedPosition);
                    if (selectList.size() == 0) {
                        initResult();
                        return;
                    }
                    adapter = null;
                    adapter = new PhotoPreviewListAdapter();
                    viewpager.setAdapter(adapter);
                    if (selectedPosition == selectList.size()) {
                        viewpager.setCurrentItem(0);
                        tv_title.setText("1/" + selectList.size());
                    } else {
                        viewpager.setCurrentItem(selectedPosition);
                    }
                    if (selectedPosition == 0) {
                        tv_title.setText("1/" + selectList.size());
                    }

                }
            });
        }
    }

    @Override
    protected ActivityPhotoPreviewBinding getLayoutView() {
        return ActivityPhotoPreviewBinding.inflate(getLayoutInflater());
    }

    @Override
    protected PhotoPreviewPresenter createPresenter() {
        return new PhotoPreviewPresenter();
    }

    private void initResult() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra("list", (ArrayList<? extends Parcelable>) selectList);
        setResult(RESULT_OK, intent);
        finish();
    }
    /**
     * 需要拦截press的事件
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            initResult();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }

    }

    public class PhotoPreviewListAdapter extends PagerAdapter {

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

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View inflater = LayoutInflater.from(PhotoPreviewActivity.this).inflate(R.layout.photo_preview_list_item, null);
            PhotoView ivIcon = inflater.findViewById(R.id.iv_icon);
            GlideUtils.INSTANCE.loadImage(PhotoPreviewActivity.this,selectList.get(position).getPath(),ivIcon);
            container.addView(inflater);
            return inflater;


        }

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

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        adapter = new PhotoPreviewListAdapter();
        viewpager.setAdapter(adapter);
    }


}
