// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/ItemTouchHelperAdapter.java
// public interface ItemTouchHelperAdapter {
// 
//     /**
//      * Called when an item has been dragged far enough to trigger a move. This is called every time
//      * an item is shifted, and not at the end of a "drop" event.
//      *
//      * @param fromPosition The start position of the moved item.
//      * @param toPosition   Then end position of the moved item.
//      * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
//      * @see RecyclerView.ViewHolder#getAdapterPosition()
//      */
//     void onItemMove(int fromPosition, int toPosition);
// 
// 
//     /**
//      * Called when an item has been dismissed by a swipe.
//      *
//      * @param position The position of the item dismissed.
//      * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
//      * @see RecyclerView.ViewHolder#getAdapterPosition()
//      */
//     void onItemDismiss(int position);
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/ItemTouchHelperViewHolder.java
// public interface ItemTouchHelperViewHolder {
// 
//     /**
//      * Called when the {@link ItemTouchHelper} first registers an item as being moved or swiped.
//      * Implementations should update the item view to indicate it's active state.
//      */
//     void onItemSelected();
// 
// 
//     /**
//      * Called when the {@link ItemTouchHelper} has completed the move or swipe, and the active item
//      * state should be cleared.
//      */
//     void onItemClear();
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/OnDragListener.java
// public interface OnDragListener {
// 
//     /**
//      * Called when a view is requesting a start of a drag.
//      *
//      * @param viewHolder The holder of the view to drag.
//      */
//     void onStartDrag(RecyclerView.ViewHolder viewHolder);
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgets/UIWidget.java
// abstract public class UIWidget {
//     public Widget widget;
//     public Context context;
// 
//     public abstract JSONObject getContent() throws JSONException;
// 
//     public int requestPermissions(Activity activity) {
//         return -1;
//     }
// 
//     public abstract WidgetSettingsFragment createSettingsFragment();
// 
// 
//     /*
//      * This method should return true if there are no prerequisites to the widget being created.
//      * Return false if the widget needs special permissions before it's created.
//      */
//     public boolean canBeCreated() {
//         return true;
//     }
// 
//     public abstract String getWidgetPreviewSecondaryHeader();
// 
//     public UIWidget(Context context, Widget widget) {
//         this.widget = widget;
//         this.context = context;
//     }
// 
//     public abstract void init();
// }


import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.silver.dan.castdemo.widgetList.ItemTouchHelperAdapter;
import com.silver.dan.castdemo.widgetList.ItemTouchHelperViewHolder;
import com.silver.dan.castdemo.widgetList.OnDragListener;
import com.silver.dan.castdemo.widgets.UIWidget;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
        int i = 0;
        for (Widget widget : widgetList) {
            widget.position = i;
            widget.savePosition();
            i++;
        }
    }

    void addWidget(Widget widget) {
        this.widgetList.add(widget);
        notifyItemInserted(widgetList.indexOf(widget));
    }

    void deleteWidget(Widget widget) {
        int index = widgetList.indexOf(widget);
        this.widgetList.remove(widget);
        notifyItemRemoved(index);
        widget.delete();
    }

    public void refreshSecondaryTitles() {
        notifyDataSetChanged();
//        http://stackoverflow.com/a/38132573/2517012
    }

    public void setWidgets(List<Widget> widgets) {
        widgetList = widgets;
    }


// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/ItemTouchHelperAdapter.java
// public interface ItemTouchHelperAdapter {
// 
//     /**
//      * Called when an item has been dragged far enough to trigger a move. This is called every time
//      * an item is shifted, and not at the end of a "drop" event.
//      *
//      * @param fromPosition The start position of the moved item.
//      * @param toPosition   Then end position of the moved item.
//      * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
//      * @see RecyclerView.ViewHolder#getAdapterPosition()
//      */
//     void onItemMove(int fromPosition, int toPosition);
// 
// 
//     /**
//      * Called when an item has been dismissed by a swipe.
//      *
//      * @param position The position of the item dismissed.
//      * @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
//      * @see RecyclerView.ViewHolder#getAdapterPosition()
//      */
//     void onItemDismiss(int position);
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/ItemTouchHelperViewHolder.java
// public interface ItemTouchHelperViewHolder {
// 
//     /**
//      * Called when the {@link ItemTouchHelper} first registers an item as being moved or swiped.
//      * Implementations should update the item view to indicate it's active state.
//      */
//     void onItemSelected();
// 
// 
//     /**
//      * Called when the {@link ItemTouchHelper} has completed the move or swipe, and the active item
//      * state should be cleared.
//      */
//     void onItemClear();
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgetList/OnDragListener.java
// public interface OnDragListener {
// 
//     /**
//      * Called when a view is requesting a start of a drag.
//      *
//      * @param viewHolder The holder of the view to drag.
//      */
//     void onStartDrag(RecyclerView.ViewHolder viewHolder);
// }
// 
// Path: app/src/main/java/com/silver/dan/castdemo/widgets/UIWidget.java
// abstract public class UIWidget {
//     public Widget widget;
//     public Context context;
// 
//     public abstract JSONObject getContent() throws JSONException;
// 
//     public int requestPermissions(Activity activity) {
//         return -1;
//     }
// 
//     public abstract WidgetSettingsFragment createSettingsFragment();
// 
// 
//     /*
//      * This method should return true if there are no prerequisites to the widget being created.
//      * Return false if the widget needs special permissions before it's created.
//      */
//     public boolean canBeCreated() {
//         return true;
//     }
// 
//     public abstract String getWidgetPreviewSecondaryHeader();
// 
//     public UIWidget(Context context, Widget widget) {
//         this.widget = widget;
//         this.context = context;
//     }
// 
//     public abstract void init();
// }


// Path: app/src/main/java/com/silver/dan/castdemo/WidgetListAdapter.java
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.silver.dan.castdemo.widgetList.ItemTouchHelperAdapter;
import com.silver.dan.castdemo.widgetList.ItemTouchHelperViewHolder;
import com.silver.dan.castdemo.widgetList.OnDragListener;
import com.silver.dan.castdemo.widgets.UIWidget;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

        int i = 0;
        for (Widget widget : widgetList) {
            widget.position = i;
            widget.savePosition();
            i++;
        }
    }

    void addWidget(Widget widget) {
        this.widgetList.add(widget);
        notifyItemInserted(widgetList.indexOf(widget));
    }

    void deleteWidget(Widget widget) {
        int index = widgetList.indexOf(widget);
        this.widgetList.remove(widget);
        notifyItemRemoved(index);
        widget.delete();
    }

    public void refreshSecondaryTitles() {
        notifyDataSetChanged();
//        http://stackoverflow.com/a/38132573/2517012
    }

    public void setWidgets(List<Widget> widgets) {
        widgetList = widgets;
    }


    class WidgetViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {