package com.friendvilla.shayaristock;

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

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

import static android.widget.Toast.LENGTH_SHORT;
import static android.widget.Toast.makeText;

public class ItemViewAdapter extends RecyclerView.Adapter<ItemViewAdapter.ViewHolder> {

    private List<ItemClass> ITEM_LIST;
    private Activity activity;
    private int lastPosition = -1;

    ItemViewAdapter(List<ItemClass> ITEM_LIST, Activity activity) {
        this.ITEM_LIST = ITEM_LIST;
        this.activity = activity;
    }

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

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final ItemClass itemClass = ITEM_LIST.get(position);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.stext.setText(Html.fromHtml(itemClass.getsText(), Html.FROM_HTML_MODE_COMPACT));
        } else {
            holder.stext.setText(Html.fromHtml(itemClass.getsText()));
        }
        holder.stime.setText(itemClass.getsCategory().toUpperCase());
        holder.copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData cData = ClipData.newPlainText("text",itemClass.getsText());
                assert cManager != null;
                cManager.setPrimaryClip(cData);
                makeText(activity,"Copied", LENGTH_SHORT).show();
            }
        });
        holder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent shareint = new Intent(Intent.ACTION_SEND);
                shareint.setType("text/plain");
                shareint.putExtra(Intent.EXTRA_SUBJECT, "Shayari");
                shareint.putExtra(Intent.EXTRA_TEXT, itemClass.getsText());
                activity.startActivity(Intent.createChooser(shareint, "Share Via"));
            }
        });
        holder.love.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.layout.setBackgroundColor(Color.WHITE);
                if (ContextCompat.checkSelfPermission(activity,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        Bitmap bitmap = loadBitmapFromView(holder.layout);
                        File dir = new File(Objects.requireNonNull(activity.getExternalFilesDir(null)).getAbsolutePath());
                        File file = new File(dir, "capture.jpg");
                        try {
                            FileOutputStream fos = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                            Uri screenshotUri = Uri.parse(dir + "/capture.jpg");
                            sharingIntent.setType("image/jpg");
                            sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                            activity.startActivity(Intent.createChooser(sharingIntent, "Share Image Using"));
                        } catch (Exception e) {
                            makeText(activity, "Error! " + e.getMessage(), LENGTH_SHORT).show();
                        }
                } else {
                    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                }
            }

        });
        setBottomAnimation(holder.itemView);
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder{

        private TextView stext,stime;
        private LinearLayout layout,share,copy,love;
        ViewHolder(View itemView) {
            super(itemView);
            stext = itemView.findViewById(R.id.text);
            stime = itemView.findViewById(R.id.time);
            share = itemView.findViewById(R.id.share);
            copy = itemView.findViewById(R.id.copy);
            love = itemView.findViewById(R.id.love);
            layout = itemView.findViewById(R.id.view);
        }
    }

    private void setBottomAnimation(View view) {
        Animation zoomIn = AnimationUtils.loadAnimation(activity, R.anim.item_from_botton);
        view.startAnimation(zoomIn);
    }

    private Bitmap loadBitmapFromView(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
        v.draw(c);
        return b;
    }

    void addMoreData(List<ItemClass> mRecyclerViewItem) {
        ITEM_LIST.addAll(mRecyclerViewItem);
        notifyDataSetChanged();
    }

}

