
package org.zarroboogs.weibo.selectphoto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.zarroboogs.weibo.R;
import org.zarroboogs.weibo.activity.ToolBarAppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ImgFileListActivity extends ToolBarAppCompatActivity implements OnItemClickListener {

    public static final int REQUEST_CODE = 0x0000;
    private SelectImgUtil util;
    private ImgFileListAdapter listAdapter;
    private List<FileTraversal> locallist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_imgdirs_activity_layout);

        ListView listView = (ListView) findViewById(R.id.imageVileListView);
        util = new SelectImgUtil(this);
        locallist = util.LocalImgFileList();
        List<HashMap<String, String>> listdata = new ArrayList<HashMap<String, String>>();
        // Bitmap bitmap[] = null;
        if (locallist != null) {
            // bitmap = new Bitmap[locallist.size()];
            for (int i = 0; i < locallist.size(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("filecount", locallist.get(i).filecontent.size() + getString(R.string.img_number));
                map.put("imgpath", locallist.get(i).filecontent.get(0) == null ? null
                        : (locallist.get(i).filecontent.get(0)));
                map.put("filename", locallist.get(i).filename);
                listdata.add(map);
            }
        }
        listAdapter = new ImgFileListAdapter(this, listdata);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(this);

        disPlayHomeAsUp(R.id.mainTimeLineToolBar);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
            case android.R.id.home:
                setResult(RESULT_OK);
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Intent intent = new Intent(this, ImgsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("data", locallist.get(arg2));
        intent.putExtras(bundle);
        startActivityForResult(intent, ImgsActivity.REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == ImgsActivity.REQUEST_CODE && resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
        // super.onActivityResult(requestCode, resultCode, data);
    }
}
