package io.github.lonamiwebs.stringlate.activities.repositories;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

import io.github.lonamiwebs.stringlate.R;
import io.github.lonamiwebs.stringlate.activities.translate.TranslateActivity;
import io.github.lonamiwebs.stringlate.classes.repos.RepoHandler;
import io.github.lonamiwebs.stringlate.classes.repos.RepoHandlerAdapter;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import static io.github.lonamiwebs.stringlate.utilities.Constants.EXTRA_REPO;

public class HistoryFragment extends Fragment {

    //region Members

    private ListView mRepositoryListView;
    private TextView mHistoryMessageTextView;
    private TextView mRepositoriesTitle;

    //endregion

    //region Initialization

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_history, container, false);

        mRepositoryListView = (ListView)rootView.findViewById(R.id.repositoryListView);
        mRepositoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                RepoHandler repo = (RepoHandler)adapterView.getItemAtPosition(i);
                launchTranslateActivity(repo);
            }
        });

        mHistoryMessageTextView = (TextView)rootView.findViewById(R.id.historyMessageTextView);
        mRepositoriesTitle = (TextView)rootView.findViewById(R.id.repositoriesTitle);

        changeListener.onRepositoryCountChanged();

        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RepoHandler.addChangeListener(changeListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RepoHandler.removeChangeListener(changeListener);
    }

    //endregion

    //region Listeners

    private final RepoHandler.ChangeListener changeListener = new RepoHandler.ChangeListener() {
        @Override
        public void onRepositoryCountChanged() {
            ArrayList<RepoHandler> repositories = RepoHandler.listRepositories(getContext());
            Collections.sort(repositories);

            if (repositories.isEmpty()) {
                mRepositoriesTitle.setVisibility(GONE);
                mHistoryMessageTextView.setText(getString(
                        R.string.history_no_repos_hint, getString(R.string.add_new_repo)));
                mRepositoryListView.setAdapter(null);
            } else {
                mRepositoriesTitle.setVisibility(VISIBLE);
                mHistoryMessageTextView.setText(R.string.history_contains_repos_hint);
                mRepositoryListView.setAdapter(
                        new RepoHandlerAdapter(getContext(), repositories));
            }
        }
    };

    //endregion

    //region Utilities

    private void launchTranslateActivity(RepoHandler repo) {
        Intent intent = new Intent(getContext(), TranslateActivity.class);
        intent.putExtra(EXTRA_REPO, repo.toBundle());
        startActivity(intent);
    }

    //endregion
}

@@ -55,24 +55,64 @@
 ositories;%0A%0A
+import android.content.DialogInterface;%0A
 import andro
@@ -236,24 +236,67 @@
 p.Fragment;%0A
+import android.support.v7.app.AlertDialog;%0A
 import andro
@@ -1883,16 +1883,1123 @@
      %7D);
+%0A        mRepositoryListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() %7B%0A            @Override%0A            public boolean onItemLongClick(AdapterView%3C?%3E parent, View view, int position, long id) %7B%0A                final RepoHandler repo = (RepoHandler)parent.getItemAtPosition(position);%0A                // TODO Copy paste from TranslateActivity%0A                new AlertDialog.Builder(getContext())%0A                        .setTitle(R.string.sure_question)%0A                        .setMessage(getString(R.string.delete_repository_confirm_long, repo.toString()))%0A                        .setPositiveButton(getString(R.string.delete_repository), new DialogInterface.OnClickListener() %7B%0A                            @Override%0A                            public void onClick(DialogInterface dialogInterface, int i) %7B%0A                                repo.delete();%0A                            %7D%0A                        %7D)%0A                        .setNegativeButton(getString(R.string.cancel), null)%0A                        .show();%0A                return true;%0A            %7D%0A        %7D);
 %0A%0A      

Allow deleting the repository from history fragment