package com.artisanic.helper.artadapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.artisanic.helper.ArtApp;
import com.artisanic.helper.artmanager.ArtKeys;
import com.artisanic.helper.artmanager.ArtUtils;
import com.artisanic.helper.artui.ListActivity;
import com.artisanic.helper.test.R;

import java.util.List;
import java.util.Random;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.HomeVH> {

    private List<String> nameList = ArtKeys.getAllDir();
    private Context mCon;
    private static final int CORNER_RADIUS = 32;

    public MainAdapter(Context mCon) {
        this.mCon = mCon;
    }

    @NonNull
    @Override
    public HomeVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_main, parent, false);
        return new HomeVH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeVH holder, int position) {
        String dir = nameList.get(position);
        String dirSimpleName = dir.substring(dir.lastIndexOf("_") + 1);
        holder.tvName.setText(dirSimpleName);
        List<String> imagePaths = ArtUtils.fileExistsInAssets(mCon, dir);
        if (!imagePaths.isEmpty()) {
            Random random = new Random();
            String randomImagePath = imagePaths.get(random.nextInt(imagePaths.size()));
            Bitmap bitmap = ArtUtils.loadImageFromAssets(mCon, randomImagePath);
            if (bitmap != null) {
                holder.ivCategory.setImageBitmap(getRoundedCornerBitmap(bitmap, CORNER_RADIUS));
            } else {
                holder.ivCategory.setImageResource(R.mipmap.ic_launcher);
            }

            holder.ivCategory.setOnClickListener(v -> {
                Intent intent = new Intent(mCon, ListActivity.class);
                intent.putExtra(ArtApp.Key_ViewDir, dir);
                mCon.startActivity(intent);
            });
        } else {
            holder.ivCategory.setImageResource(R.mipmap.ic_launcher);
        }
    }
    private Bitmap getRoundedCornerBitmap(Bitmap bitmap, int cornerRadius) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());

        paint.setAntiAlias(true);
        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);

        paint.setXfermode(new android.graphics.PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (bitmap != output) {
            bitmap.recycle();
        }
        return output;
    }

    @Override
    public int getItemCount() {
        return nameList.size();
    }

    public static class HomeVH extends RecyclerView.ViewHolder {
        private TextView tvName;
        private ImageView ivCategory;

        public HomeVH(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivCategory = itemView.findViewById(R.id.iv_category);
        }
    }
}