package app.insti.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

import app.insti.Constants;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.api.model.BroadActivity;
import app.insti.api.model.Event;
import app.insti.fragment.BroadActivityFragment;
import app.insti.fragment.ExploreFragment;
import app.insti.fragment.FeedFragment;
import app.insti.fragment.SubActivityFragment;
import app.insti.fragment.WebViewFragment;

public class ActivitiesAdapter extends RecyclerView.Adapter<ActivitiesAdapter.BroadActivitiesViewHolder> {

    private Context context;
    private List<BroadActivity> broadActivityList;

    public ActivitiesAdapter(Context context,List<BroadActivity> broadActivityList){
        this.context = context;
        this.broadActivityList = broadActivityList;
    }

    @NonNull
    @Override
    public BroadActivitiesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.resource_layout_broad_activities,parent,false);
        return new BroadActivitiesViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BroadActivitiesViewHolder holder, int position) {
        BroadActivity broadActivity = broadActivityList.get(position);
        holder.txtView.setText(broadActivity.getBroadActivityName());
        Picasso.get().load(broadActivity.getAvatarUrl()).fit().centerCrop().into(holder.imgView);
        holder.linearLayoutActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Toast.makeText(context,  broadActivity.getBroadActivityName() + " clicked", Toast.LENGTH_SHORT).show();
//              String broadActivityName = broadActivity.getBroadActivityName();
//              String broadActivityImageURL = broadActivity.getAvatarUrl();
//                AppCompatActivity activity = (AppCompatActivity) v.getContext();
////                SubActivityFragment subActivityFragment = new SubActivityFragment();
//                ExploreFragment feedFragment = new ExploreFragment();
//                activity.getSupportFragmentManager().beginTransaction().replace(R.id.broadActivitiesList,feedFragment).addToBackStack(null).commit();

            }
        });
    }

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

    public class BroadActivitiesViewHolder extends RecyclerView.ViewHolder {
        ImageView imgView;
        TextView txtView;
        LinearLayout linearLayoutActivity;
        public BroadActivitiesViewHolder(@NonNull View itemView) {
            super(itemView);
            imgView = itemView.findViewById(R.id.imgViewActivity);
            txtView = itemView.findViewById(R.id.txtViewActivity);
            linearLayoutActivity = itemView.findViewById(R.id.linearLayoutActivity);

        }
    }
}
