package org.keyscience.adapters;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.keyscience.R;
import org.keyscience.models.CspLearnModel;

import java.util.List;

public class CspLearnAdapter extends RecyclerView.Adapter<CspLearnAdapter.CspViewHolder> {

    private List<CspLearnModel> mData;
    private Context context;
    private CardView cardView;

    public int pos;

    public CspLearnAdapter(List<CspLearnModel> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }


    @NonNull
    @Override
    public CspViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.csp_learn_item, viewGroup, false);

        cardView = view.findViewById(R.id.labs_cardview);

        switch(i) {
            case EVEN:
                view = LayoutInflater.from(context).inflate(R.layout.csp_learn_item_rtl, viewGroup, false);
                break;

            case ODD:
                view = LayoutInflater.from(context).inflate(R.layout.csp_learn_item, viewGroup, false);
                break;
        }

        return new CspViewHolder(view);
    }

    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }

    @Override
    public void onBindViewHolder(@NonNull CspViewHolder cspViewHolder, int i) {
        cspViewHolder.setData(mData.get(i));
        cspViewHolder.setListeners();
    }

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

    private final int ODD=0, EVEN=1;
    @Override
    public int getItemViewType(int position) {
        pos = position;

        if (position % 2 == 0)
            return ODD;
        else
            return EVEN;
    }

    class CspViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CardView cardView;
        ImageView imageView;
        TextView title;
//        TextView date;
        String content;
        Bitmap bitmap;

        String GUID;

        public CspViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.labs_image);
            title = itemView.findViewById(R.id.labs_title);
//            date = itemView.findViewById(R.id.CspLearn_date);
            cardView = itemView.findViewById(R.id.labs_cardview);

            if ( pos == mData.size()-1 )
            {
                ViewGroup.MarginLayoutParams layoutParams =
                        (ViewGroup.MarginLayoutParams) cardView.getLayoutParams();
                layoutParams.setMargins(0, dpToPx(-3), 0, 0);
                cardView.requestLayout();
            }
        }

        public int dpToPx(int dp) {
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        }

        public void setData(final CspLearnModel model) {
            if ( model.getImage() == null )
                imageView.setImageResource(R.drawable.stony_coral_id_lecture);
            else
                imageView.setImageBitmap(model.getImage());

            title.setText(model.getTitle());
            content = model.getContent();

            bitmap = model.getImage();

            GUID = model.getGUID();
        }

        public void setListeners() {
            cardView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(GUID));
            context.startActivity(browserIntent);

//            Intent intent = new Intent(context, CspLearnActivity.class);
//            intent.putExtra("title", title.getText().toString());
//            intent.putExtra("content", content);
//            if (bitmap != null) {
//                // Resizing the resolution of larger images before converting the bitmap to byte
//                // array prevents crashing on older devices at time of conversion
//                int width = 380;
//                int height = (bitmap.getHeight() * width / bitmap.getWidth());
//                Bitmap lowres_bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
//
//
//                //convert bitmap to byte array
//                ByteArrayOutputStream stream = new ByteArrayOutputStream();
//
//                // Images are either JPEG or PNG formats. I noticed PNG causes a crash for JPG
//                try {
//                    lowres_bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//                } catch (Exception o) {
//                    lowres_bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
//                }
//                byte[] byteArray = stream.toByteArray();
//                intent.putExtra("image", byteArray);
//            }
//            context.startActivity(intent);
        }

    }
}


