package com.napontaratan.vibernate;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.daimajia.swipe.SwipeLayout;
import com.napontaratan.vibernate.model.TimerConflictException;
import com.napontaratan.vibernate.model.TimerSession;
import com.napontaratan.vibernate.model.TimerSessionHolder;
import com.napontaratan.vibernate.model.TimerUtils;


/**
 * Created by Aor-Nawattranakul on 15-04-06.
 */
public class vAdapter extends RecyclerView.Adapter<vAdapter.vViewHolder> {
    private LayoutInflater inflater;
    private TimerSessionHolder timerSessionHolder;
    private Context context;
  
    public vAdapter(Context context, TimerSessionHolder timerSessionHolder) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.timerSessionHolder = timerSessionHolder;
        this.timerSessionHolder.setAdapter(this);
    }

    @Override
    public vViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.v_row, parent, false); // inflat xml into a view
        vViewHolder holder = new vViewHolder(view); // turn a view into viewHolder storing specified values
        return holder;
    }

    @Override
    public void onBindViewHolder(vViewHolder holder, int position) {
        TimerSession current = timerSessionHolder.get(position);
        holder.description.setText(current.getName());
        holder.startTime.setText(TimerUtils.getStartTimeInFormat(current, "HH:mm"));
        holder.endTime.setText(TimerUtils.getEndTimeInFormat(current, "HH:mm"));
        holder.activeDays.setText(TimerUtils.getDaysInFormat(current));
        if (current.getType() == TimerSession.TimerSessionType.SILENT) {
            holder.typeIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_silent));
        } else {
            holder.typeIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_vibrate));
        }
        holder.colorTab.setBackgroundColor(current.getColor());
        vAnimate.animate(holder);
    }

    @Override
    public int getItemCount() {
        return timerSessionHolder.getSize();
    }


    private void removeItem(int position) {
        timerSessionHolder.removeTimer(position);
        notifyItemRemoved(position);
    }

    public void addItem(TimerSession timer) {
        try {
            timerSessionHolder.addTimer(timer);
            notifyItemInserted(timerSessionHolder.getSize());
        } catch (TimerConflictException e) {
            System.out.println("Timer Conflict please check your time again");
        }
    }

    public class vViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnTouchListener {
        TextView description;
        TextView startTime;
        TextView endTime;
        TextView activeDays;
        ImageView typeIcon;
        View colorTab;
        View box;
        SwipeLayout swipeLayout;

        public vViewHolder(View itemView) {
            super(itemView);
            description = (TextView) itemView.findViewById(R.id.v_description);
            startTime = (TextView) itemView.findViewById(R.id.v_startTime);
            endTime = (TextView) itemView.findViewById(R.id.v_endTime);
            typeIcon = (ImageView) itemView.findViewById(R.id.v_mute_icon);
            activeDays = (TextView) itemView.findViewById(R.id.v_show_activeDays);
            colorTab = itemView.findViewById(R.id.TSisActive);
            box = itemView.findViewById(R.id.click_area);
            box.setOnClickListener(this);
            typeIcon.setOnClickListener(this);
            colorTab.setOnClickListener(this);
            swipeLayout = (SwipeLayout) itemView.findViewById(R.id.listview_swipe_layout);
            swipeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    swipeLayout.toggle();
                }
            });
        }

        @Override
        public void onClick(View v) {
//            if (v == typeIcon) {
//                Toast.makeText(context, "Timer" + getPosition() + "is muted", Toast.LENGTH_SHORT).show();
//            } else if (v == box) {
//                Intent mIntent = new Intent(v.getContext(), CreateTimerActivity.class);
//                Bundle mBundle = new Bundle();
//                mBundle.putSerializable("Timer", TimerSessionHolder.getInstance().get(getPosition()));
//                mIntent.putExtras(mBundle);
//                v.getContext().startActivity(mIntent);
//            } else if (v == colorTab) {
//                removeItem(getPosition());
//
//            }
            swipeLayout.toggle();

        }


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return false;
        }
    }
}

@@ -3318,16 +3318,74 @@
 eLayout;
+%0A        ImageView editIcon;%0A        ImageView deleteIcon;
 %0A%0A      
@@ -4202,218 +4202,166 @@
     
-swipeLayout.setOnClickListener(new View.OnClickListener() %7B%0A                @Override%0A                public void onClick(View view) %7B%0A                    swipeLayout.toggle();%0A                %7D%0A            %7D);
+editIcon = (ImageView) itemView.findViewById(R.id.listview_edit_icon);%0A            deleteIcon = (ImageView) itemView.findViewById(R.id.listview_delete_icon);%0A
 %0A   
@@ -4420,26 +4420,24 @@
 k(View v) %7B%0A
-//
             
@@ -4449,22 +4449,23 @@
  == 
-typeIcon
+swipeLayout
 ) %7B%0A
-//
     
@@ -4480,100 +4480,30 @@
     
-Toast.makeText(context, %22Timer%22 + getPosition() + %22is muted%22, Toast.LENGTH_SHORT).show
+swipeLayout.toggle
 ();%0A
-//
     
@@ -4530,17 +4530,20 @@
  == 
-box
+editIcon
 ) %7B%0A
-//
     
@@ -4626,18 +4626,16 @@
 class);%0A
-//
         
@@ -4673,18 +4673,16 @@
 ndle();%0A
-//
         
@@ -4776,18 +4776,16 @@
 on()));%0A
-//
         
@@ -4820,18 +4820,16 @@
 undle);%0A
-//
         
@@ -4875,18 +4875,16 @@
 ntent);%0A
-//
         
@@ -4907,22 +4907,22 @@
  == 
-colorTab
+deleteIcon
 ) %7B%0A
-//
     
@@ -4937,87 +4937,22 @@
     
-removeItem(getPosition());%0A//%0A//            %7D%0A            swipeLayout.toggle();
+%0A            %7D
 %0A%0A  

refactor code in vAdapter: make sure the functionality of swipelayout works