package fr.free.nrw.commons.contributions;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import fr.free.nrw.commons.R;
import fr.free.nrw.commons.upload.UploadService;
import timber.log.Timber;

import static fr.free.nrw.commons.contributions.Contribution.STATE_FAILED;

class ContributionsListAdapter extends CursorAdapter {

    private final ContributionDao contributionDao;
    private Cursor allContributions;
    private UploadService uploadService;

    public ContributionsListAdapter(Context context, Cursor c, int flags, ContributionDao contributionDao) {
        super(context, c, flags);
        this.contributionDao = contributionDao;
    }

    public void setUploadService(Cursor allContributions, UploadService uploadService) {
        this.allContributions = allContributions;
        this.uploadService = uploadService;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        View parent = LayoutInflater.from(context)
                .inflate(R.layout.layout_contribution, viewGroup, false);
        parent.setTag(new ContributionViewHolder(parent));
        return parent;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final ContributionViewHolder views = (ContributionViewHolder)view.getTag();
        final Contribution contribution = contributionDao.fromCursor(cursor);

        views.imageView.setMedia(contribution);
        views.titleView.setText(contribution.getDisplayTitle());

        views.seqNumView.setText(String.valueOf(cursor.getPosition() + 1));
        views.seqNumView.setVisibility(View.VISIBLE);
        views.position = cursor.getPosition();


        switch (contribution.getState()) {
            case Contribution.STATE_COMPLETED:
                views.stateView.setVisibility(View.GONE);
                views.progressView.setVisibility(View.GONE);
                views.retryButton.setVisibility(View.GONE);
                views.cancelButton.setVisibility(View.GONE);
                views.stateView.setText("");
                break;
            case Contribution.STATE_QUEUED:
                views.stateView.setVisibility(View.VISIBLE);
                views.progressView.setVisibility(View.GONE);
                views.stateView.setText(R.string.contribution_state_queued);
                views.retryButton.setVisibility(View.GONE);
                views.cancelButton.setVisibility(View.GONE);
                break;
            case Contribution.STATE_IN_PROGRESS:
                views.stateView.setVisibility(View.GONE);
                views.progressView.setVisibility(View.VISIBLE);
                views.retryButton.setVisibility(View.GONE);
                views.cancelButton.setVisibility(View.GONE);
                long total = contribution.getDataLength();
                long transferred = contribution.getTransferred();
                if (transferred == 0 || transferred >= total) {
                    views.progressView.setIndeterminate(true);
                } else {
                    views.progressView.setProgress((int)(((double)transferred / (double)total) * 100));
                }
                break;
            case Contribution.STATE_FAILED:
                views.stateView.setVisibility(View.VISIBLE);
                views.stateView.setText(R.string.contribution_state_failed);
                views.progressView.setVisibility(View.GONE);
                views.retryButton.setVisibility(View.VISIBLE);
                views.cancelButton.setVisibility(View.VISIBLE);

                views.retryButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.d("deneme","contex:"+context);
                        Log.d("deneme","Retry button is clicked");
                        retryUpload(cursor);
                    }
                });

                views.cancelButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.d("deneme","Cancel button is clicked");
                    }
                });


                break;
        }
    }

    /**
     * Retry upload when it is failed
     * @param cursor position of upload which will be retried
     */
    public void retryUpload(Cursor cursor) {
        // TODO: first check for internet connection, if not display a message and do nothing.
        Log.d("deneme","Retrying i:"+cursor.getColumnName(1));
        Contribution c = contributionDao.fromCursor(cursor);
        if (c.getState() == STATE_FAILED) {
            uploadService.queue(UploadService.ACTION_UPLOAD_FILE, c);
            Timber.d("Restarting for %s", c.toString());
            Log.d("deneme","Restarting for %s"+ c.getFilename());

        } else {
            Timber.d("Skipping re-upload for non-failed %s", c.toString());
            Log.d("deneme","Skipping re-upload for non-failed %s"+ c.getFilename());

        }
    }
}

@@ -4346,32 +4346,78 @@
 n is clicked%22);%0A
+                        deleteUpload(cursor);%0A
                 
@@ -5296,11 +5296,752 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Delete a failed upload attempt%0A     * @param cursor position of upload attempt which will be deteled%0A     */%0A    public void deleteUpload(Cursor cursor) %7B%0A        // TODO: check internet connection, warn user and do nothing is a problem occurred%0A%0A        Contribution c = contributionDao.fromCursor(cursor);%0A        if (c.getState() == STATE_FAILED) %7B%0A            Timber.d(%22Deleting failed contrib %25s%22, c.toString());%0A            Log.d(%22deneme%22,%22Deleting failed:%22+c.getFilename());%0A            contributionDao.delete(c);%0A        %7D else %7B%0A            Timber.d(%22Skipping deletion for non-failed contrib %25s%22, c.toString());%0A            Log.d(%22deneme%22,%22Skipping deletion for non-failed contrib%22+c.getFilename());%0A        %7D%0A    %7D
 %0A%7D%0A

Add cancel method too