package com.ycgis.macall.personalcenter.v.photoview;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.rs.macall.androidx.basemodel.callback.SimpleListClickListener;
import com.rs.macall.androidx.basemodel.utils.ToastUtil;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.v.custom.HorizonSlideRecycleView;

import java.io.File;
import java.util.List;

public class ShowPhotoView {
    private Context context;
    private List<String> fileArray;
    private Dialog dialog;
    private LinearLayoutManager manager;
    private  PhotoViewAdapter adapter;

    public ShowPhotoView(Context context){
        this.context = context;
    }

    public void setPhotoList(List<String> fileArray){
        this.fileArray = fileArray;
    }

    public void showPhoto(int pointer){
        if (fileArray == null || fileArray.isEmpty()){
            ToastUtil.centered(context,"照片不存在！");
            return;
        }
        File file = new File(fileArray.get(pointer));
        if (!file.exists()){
            ToastUtil.centered(context,"照片不存在！");
            return;
        }
        if (dialog == null){
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_photo_view,null);
            HorizonSlideRecycleView rv_photo = view.findViewById(R.id.rv_photo);
            manager = new LinearLayoutManager(context);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv_photo.setLayoutManager(manager);
            adapter = new PhotoViewAdapter(context,fileArray);
            rv_photo.setAdapter(adapter);

            dialog = new Dialog(context);
            dialog.setContentView(view);
            dialog.setCancelable(true);
            Window window = dialog.getWindow();
            assert window != null;
            //设置在屏幕下边
            window.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;
            //消除边距
            window.setBackgroundDrawable(null);
            window.setAttributes(params);
        }
        if (adapter!= null){
            adapter.upData(fileArray);
            if (pointer >0){
                manager.scrollToPositionWithOffset(pointer, 0);
                manager.setStackFromEnd(true);
            }
        }
        dialog.show();
        adapter.setListClickListener(new SimpleListClickListener() {
            @Override
            public void onSingleClick(View v, int pointer) {
                dialog.dismiss();
            }

            @Override
            public void onFastClick(View v, int pointer) {

            }
        });
    }
}
