/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.dashboard.suggestions;

import android.content.Context;
import android.service.settings.suggestions.Suggestion;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.drawer.Tile;
import com.android.settingslib.suggestions.SuggestionParser;

public class SuggestionDismissController extends ItemTouchHelper.SimpleCallback {

    public interface Callback {

        /**
         * @deprecated in favor of {@link #getSuggestionAt(int)}
         * Returns suggestion tile data from the callback
         */
        @Deprecated
        Tile getSuggestionForPosition(int position);

        /**
         * @deprecated in favor of {@link #onSuggestionDismissed(Suggestion)}
         * Called when a suggestion is dismissed.
         */
        @Deprecated
        void onSuggestionDismissed(Tile suggestion);

        /**
         * Returns suggestion tile data from the callback
         */
        Suggestion getSuggestionAt(int position);

        /**
         * Called when a suggestion is dismissed.
         */
        void onSuggestionDismissed(Suggestion suggestion);
    }

    private final Context mContext;
    private final SuggestionFeatureProvider mSuggestionFeatureProvider;
    private final SuggestionControllerMixin mSuggestionMixin;

    /**
     * @deprecated in favor of the new Suggestion backend.
     */
    @Deprecated
    private final SuggestionParser mSuggestionParser;
    private final Callback mCallback;

    public SuggestionDismissController(Context context, RecyclerView recyclerView,
            SuggestionControllerMixin suggestionMixin, SuggestionParser parser, Callback callback) {
        super(0, ItemTouchHelper.START | ItemTouchHelper.END);
        mSuggestionMixin = suggestionMixin;
        mContext = context;
        mSuggestionParser = parser;
        mSuggestionFeatureProvider = FeatureFactory.getFactory(context)
                .getSuggestionFeatureProvider(context);
        mCallback = callback;
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(this);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
            RecyclerView.ViewHolder target) {
        return true;
    }

    @Override
    public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        final int layoutId = viewHolder.getItemViewType();
        if (layoutId == R.layout.suggestion_tile
                || layoutId == R.layout.suggestion_tile_with_button) {
            // Only return swipe direction for suggestion tiles. All other types are not swipeable.
            return super.getSwipeDirs(recyclerView, viewHolder);
        }
        return 0;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        if (mCallback == null) {
            return;
        }
        final int position = viewHolder.getAdapterPosition();
        final Suggestion suggestionV2 = mCallback.getSuggestionAt(position);
        if (suggestionV2 != null) {
            mSuggestionFeatureProvider.dismissSuggestion(mContext, mSuggestionMixin, suggestionV2);
            mCallback.onSuggestionDismissed(suggestionV2);
        } else {
            final Tile suggestion = mCallback.getSuggestionForPosition(position);
            mSuggestionFeatureProvider.dismissSuggestion(mContext, mSuggestionParser, suggestion);
            mCallback.onSuggestionDismissed(suggestion);
        }
    }
}
