package org.keyscience.adapters;

import android.content.Context;
import android.graphics.Point;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.keyscience.R;
import org.keyscience.app.HomeActivity;
import org.keyscience.models.HomePageItem;

import java.util.List;

public class HomeItemsAdapter extends RecyclerView.Adapter<HomeItemsAdapter.MyViewHolder> {

    private List<HomePageItem> mData;
    private LayoutInflater mInflater;
    private Context context;
    private int gridLevel;

    public HomeItemsAdapter(List<HomePageItem> mData, Context context, int gridLevel) {
        this.mData = mData;
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
        this.gridLevel = gridLevel;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.list_item_home, viewGroup, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        HomePageItem currentObj = mData.get(i);
        myViewHolder.setData(currentObj, i);
        myViewHolder.setListeners();
    }

    // only use if removing content
    public void removeItem(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mData.size());
    }

    // only use if adding content
    public void addItem(int position, HomePageItem landscape) {
        mData.add(position, landscape);
        notifyItemInserted(position);
        notifyItemRangeChanged(position, mData.size());
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView imageView;
        ImageView shadowImage;
        TextView title;
        TextView subTitle;
        int position;
        HomePageItem current;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.imageHomeItem);
            this.shadowImage = itemView.findViewById(R.id.shadowImage);
            this.title = itemView.findViewById(R.id.tvTitle);
            this.subTitle = itemView.findViewById(R.id.tvSubTitle);
        }

        public void setData(HomePageItem currentObj, int i) {
            subTitle.setText(currentObj.getSubTitle());
            imageView.setImageResource(currentObj.getImageID());
            title.setText(currentObj.getTitle());
            // resize images to fit screen
            Display display = ((HomeActivity) context).getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width / 2 - 10, width / 2 - 10);
            shadowImage.setLayoutParams(layoutParams);
            imageView.setLayoutParams(layoutParams);

            position = i;
            current = currentObj;
        }

        @Override
        public void onClick(View view) {
            // must add 1 to position to offset to correct fragment since recyclerview
            // at home page dont have a home item.
            if ( gridLevel == 0 )
                ((HomeActivity) context).setViewPager(position + 1);
            else
                ((HomeActivity) context).setViewPager(position + 5);

            Toast.makeText(context, subTitle.getText(), Toast.LENGTH_SHORT).show();
        }

        public void setListeners() {
            imageView.setOnClickListener(MyViewHolder.this);
        }
    }
}
