package com.nutomic.syncthingandroid.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;

import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.activities.SettingsActivity;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import com.nutomic.syncthingandroid.util.FoldersAdapter;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Displays a list of all existing folders.
 */
public class FoldersFragment extends ListFragment implements SyncthingService.OnApiChangeListener,
        AdapterView.OnItemClickListener {

    private FoldersAdapter mAdapter;

    private Timer mTimer;

    @Override
    public void onResume() {
        super.onResume();
        setListShown(true);
    }

    @Override
    public void onApiChange(SyncthingService.State currentState) {
        if (currentState != SyncthingService.State.ACTIVE)
            return;

        initAdapter();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initAdapter();
    }

    private void initAdapter() {
        MainActivity activity = (MainActivity) getActivity();
        if (activity == null || activity.getApi() == null)
            return;

        mAdapter = new FoldersAdapter(activity);
        mAdapter.add(activity.getApi().getFolders());
        setListAdapter(mAdapter);
        setEmptyText(getString(R.string.folder_list_empty));
        getListView().setOnItemClickListener(this);
    }

    private void updateList() {
        if (mAdapter == null || getView() == null)
            return;

        MainActivity activity = (MainActivity) getActivity();
        mAdapter.updateModel(activity.getApi(), getListView());
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    updateList();
                }

            }, 0, SyncthingService.GUI_UPDATE_INTERVAL);
        } else if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(getActivity(), SettingsActivity.class)
                .setAction(SettingsActivity.ACTION_REPO_SETTINGS_FRAGMENT)
                .putExtra(SettingsActivity.EXTRA_IS_CREATE, false)
                .putExtra(FolderSettingsFragment.EXTRA_REPO_ID, mAdapter.getItem(i).ID);
        startActivity(intent);
    }

}

@@ -92,17 +92,72 @@
 oid.
-os.Bundle
+net.Uri;%0Aimport android.os.Bundle;%0Aimport android.os.Environment
 ;%0Aim
@@ -194,24 +194,49 @@
 stFragment;%0A
+import android.util.Log;%0A
 import andro
@@ -815,16 +815,53 @@
 Listener
+, AdapterView.OnItemLongClickListener
  %7B%0A%0A    
@@ -1802,16 +1802,72 @@
 (this);%0A
+        getListView().setOnItemLongClickListener(this);%0A
     %7D%0A%0A 
@@ -3026,16 +3026,16 @@
 i).ID);%0A
-
         
@@ -3064,10 +3064,399 @@
 %0A    %7D%0A%0A
+    /**%0A     * Opens the folder path with a user selected app.%0A     */%0A    @Override%0A    public boolean onItemLongClick(AdapterView%3C?%3E adapterView, View view, int i, long l) %7B%0A        Intent intent = new Intent(Intent.ACTION_VIEW);%0A        Uri uri = Uri.parse(mAdapter.getItem(i).Path);%0A        intent.setDataAndType(uri, %22*/*%22);%0A        startActivity(intent);%0A        return true;%0A    %7D%0A%0A
 %7D%0A

Allow opening folder in other app by long clicking (fixes #175).