package de.beyondsoft.ownchat.filepicker.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;

import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.filepicker.PickerManager;
import de.beyondsoft.ownchat.filepicker.models.ImageDirectory;
import de.beyondsoft.ownchat.filepicker.utils.AndroidLifecycleUtils;
import de.beyondsoft.ownchat.utils.ui.GlideApp;

public class FolderGridAdapter extends SelectableAdapter<FolderGridAdapter.ImageViewHolder, ImageDirectory> {

    private final Context context;
    private final boolean showCamera;
    private int imageSize;

    private final static int ITEM_TYPE_CAMERA = 100;
    private final static int ITEM_TYPE_PHOTO  = 101;
    private FolderGridAdapterListener folderGridAdapterListener;

    public interface FolderGridAdapterListener{
        void onCameraClicked();
        void onFolderClicked(ImageDirectory photoDirectory);
    }

    public FolderGridAdapter(Context context, ArrayList<ImageDirectory> photos, ArrayList<String> selectedPaths, boolean showCamera) {
        super(photos, selectedPaths);
        this.context = context;
        this.showCamera = showCamera;
        setColumnNumber(context,3);
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_folder_layout, parent, false);

        return new ImageViewHolder(itemView);
    }

    @Override
    public int getItemViewType(int position) {
        if(showCamera) {
            return (position == 0) ? ITEM_TYPE_CAMERA : ITEM_TYPE_PHOTO;
        } else {
            return ITEM_TYPE_PHOTO;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final ImageViewHolder holder, int position) {
        if(getItemViewType(position) == ITEM_TYPE_PHOTO) {
            final ImageDirectory photoDirectory = getItems().get(showCamera?position-1:position);

            if(AndroidLifecycleUtils.canLoadImage(holder.imageView.getContext())) {
                GlideApp.with(holder.imageView.getContext())
                        .asBitmap()
                        .load(new File(photoDirectory.getCoverPath()))
                        .override(imageSize, imageSize)
                        .thumbnail(.25f)
                        .into(holder.imageView);
            }

            holder.folderTitle.setText(photoDirectory.getName());
            holder.folderCount.setText(String.valueOf(photoDirectory.getMedias().size()));

            holder.itemView.setOnClickListener(view -> {
                if(folderGridAdapterListener!=null) {
                    folderGridAdapterListener.onFolderClicked(photoDirectory);
                }
            });

            holder.bottomOverlay.setVisibility(View.VISIBLE);
        } else {
            holder.imageView.setImageResource(PickerManager.getInstance().getCameraDrawable());
            holder.itemView.setOnClickListener(view -> {
                if(folderGridAdapterListener!=null) {
                    folderGridAdapterListener.onCameraClicked();
                }
            });

            holder.bottomOverlay.setVisibility(View.GONE);
        }
    }

    private void setColumnNumber(Context context, int columnNum) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();

        if(wm != null) {
            wm.getDefaultDisplay().getMetrics(metrics);
        }

        int widthPixels = metrics.widthPixels;
        imageSize = widthPixels / columnNum;
    }

    @Override
    public int getItemCount() {
        if(showCamera) {
            return getItems().size() + 1;
        }

        return getItems().size();
    }

    public void setFolderGridAdapterListener(FolderGridAdapterListener onClickListener) {
        this.folderGridAdapterListener = onClickListener;
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView folderTitle;
        TextView folderCount;
        View bottomOverlay;
        View selectBg;

        ImageViewHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.iv_photo);
            folderTitle = itemView.findViewById(R.id.folder_title);
            folderCount = itemView.findViewById(R.id.folder_count);
            bottomOverlay = itemView.findViewById(R.id.bottomOverlay);
            selectBg = itemView.findViewById(R.id.transparent_bg);
        }
    }
}
