package org.keyscience.adapters;

import android.content.Context;
import android.content.Intent;
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.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import org.keyscience.R;
import org.keyscience.app.Constants;
import org.keyscience.app.ExploreTheKey;
import org.keyscience.app.HomeActivity;
import org.keyscience.app.ReviewMapActivity;
import org.keyscience.models.CspLabModel;

import java.util.ArrayList;

public class CspLabAdapter extends RecyclerView.Adapter<CspLabAdapter.CspLabViewHolder> {

    private ArrayList<CspLabModel> data;
    private Context context;
    private LayoutInflater mInflater;

    public CspLabAdapter(ArrayList<CspLabModel> data, Context context) {
        this.context = context;
        this.data = data;
        this.mInflater = LayoutInflater.from(context);
    }

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

    @Override
    public void onBindViewHolder(@NonNull CspLabViewHolder viewHolder, int i) {
        viewHolder.setData(data.get(i));
        viewHolder.setListeners();
    }

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

    class CspLabViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView imageView;
        ImageView shadowImage;
        TextView title;

        CspLabViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.csp_image);
            shadowImage = itemView.findViewById(R.id.shadowImageCspLab);
            title = itemView.findViewById(R.id.csp_title);
        }

        public void setData(CspLabModel cspLabModel) {
            imageView.setImageResource(cspLabModel.getImage());
            title.setText(cspLabModel.getTitle());

            // resize images to fit screen
            Display display = ((HomeActivity) context).getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width / 2 - 10, width / 2 - 10);
            shadowImage.setLayoutParams(layoutParams);
            imageView.setLayoutParams(layoutParams);
        }

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

        @Override
        public void onClick(View view) {
            switch (title.getText().toString()) {
                case Constants.CSP_EXPLORE:
                    Intent intent = new Intent(context, ExploreTheKey.class);
                    context.startActivity(intent);
                    break;
                case Constants.CSP_LEARN:
                    ((HomeActivity) context).setViewPager(ExpListAdapter.getFragmentMap().get(Constants.CSP_LEARN));
                    break;
                case Constants.CSP_RECORD:
                    ((HomeActivity) context).setViewPager(ExpListAdapter.getFragmentMap().get(Constants.CSP_RECORD));
                    break;
                case Constants.CSP_REVIEW:
                    Intent review = new Intent(context, ReviewMapActivity.class);
                    context.startActivity(review);
                    break;
            }
        }
    }
}
