package com.satsumasoftware.timetable.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.satsumasoftware.timetable.R;
import com.satsumasoftware.timetable.db.util.ClassUtils;
import com.satsumasoftware.timetable.db.util.SubjectUtils;
import com.satsumasoftware.timetable.framework.Class;
import com.satsumasoftware.timetable.framework.ClassDetail;
import com.satsumasoftware.timetable.framework.ClassTime;
import com.satsumasoftware.timetable.framework.Color;
import com.satsumasoftware.timetable.framework.Subject;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class HomeClassesAdapter extends RecyclerView.Adapter<HomeClassesAdapter.ClassesViewHolder> {

    private Context mContext;
    private ArrayList<ClassTime> mClassTimes;

    public HomeClassesAdapter(Context context, ArrayList<ClassTime> classTimes) {
        mContext = context;
        mClassTimes = classTimes;
    }

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

    @Override
    public void onBindViewHolder(ClassesViewHolder holder, int position) {
        ClassTime classTime = mClassTimes.get(position);
        ClassDetail classDetail = ClassUtils.getClassDetailWithId(mContext, classTime.getClassDetailId());
        Class cls = ClassUtils.getClassWithId(mContext, classDetail.getClassId());
        assert cls != null;

        Subject subject = SubjectUtils.getSubjectWithId(mContext, cls.getSubjectId());
        assert subject != null;
        holder.mSubject.setText(subject.getName());

        Color color = new Color(subject.getColorId());
        holder.mColorCircle.setImageResource(color.getPrimaryColorResId(mContext));

        StringBuilder detailBuilder = new StringBuilder();
        detailBuilder
                .append(classTime.getStartTime().toString())
                .append(" - ")
                .append(classTime.getEndTime().toString())
                .append(" \u2022 ")
                .append(classDetail.getRoom());

        holder.mDetails.setText(detailBuilder.toString());
    }

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

    public class ClassesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        CircleImageView mColorCircle;
        TextView mSubject, mDetails;

        ClassesViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mColorCircle = (CircleImageView) itemView.findViewById(R.id.imageView);
            mSubject = (TextView) itemView.findViewById(R.id.subject);
            mDetails = (TextView) itemView.findViewById(R.id.details);
        }

        @Override
        public void onClick(View view) {
            if (mOnEntryClickListener != null) {
                mOnEntryClickListener.onEntryClick(view, getLayoutPosition());
            }
        }
    }

    private OnEntryClickListener mOnEntryClickListener;

    public interface OnEntryClickListener {
        void onEntryClick(View view, int position);
    }

    public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
        mOnEntryClickListener = onEntryClickListener;
    }

}

@@ -2097,24 +2097,25 @@
 gBuilder();%0A
+%0A
         deta
@@ -2123,33 +2123,16 @@
 lBuilder
-%0A                
 .append(
@@ -2249,32 +2249,34 @@
 me().toString())
+;%0A
 %0A               
@@ -2264,32 +2264,107 @@
 );%0A%0A        
-        
+if (classDetail.hasRoom() %7C%7C classDetail.hasBuilding()) %7B%0A            detailBuilder
 .append(%22 %5Cu
@@ -2370,16 +2370,18 @@
 u2022 %22)
+;%0A
 %0A       
@@ -2389,43 +2389,320 @@
     
-    .append(classDetail.getRoom());
+if (classDetail.hasRoom()) %7B%0A                detailBuilder.append(classDetail.getRoom());%0A                if (classDetail.hasBuilding()) detailBuilder.append(%22, %22);%0A            %7D%0A            if (classDetail.hasBuilding()) %7B%0A                detailBuilder.append(classDetail.getBuilding());%0A            %7D%0A        %7D
 %0A%0A  

Improve how class details are displayed on the home screen card