package com.example.logistics.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.example.logistics.R;
import com.example.logistics.manager.operationManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HistoryActivity extends Activity implements OnItemClickListener, OnScrollListener, View.OnClickListener  {

    private String TAG = "HistoryActivity";
    private ListView lv_history;
    private ArrayAdapter<String> arr_adapter;
    private SimpleAdapter simp_adapter;
    private String phonenum;
    private List<Map<String, Object>> dataList;
    private ImageButton sync_his;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        sync_his = (ImageButton) findViewById(R.id.sync_his);
        dataList=new ArrayList<Map<String,Object>>();
        lv_history = (ListView) findViewById(R.id.lv_history);

        simp_adapter=new SimpleAdapter(this, dataList, R.layout.item, new String[]{"pickupcode","time"}, new int[]{R.id.np_id,R.id.np_time});
        lv_history.setAdapter(simp_adapter);
        lv_history.setOnItemClickListener(this);
        lv_history.setOnScrollListener(this);
        sync_his.setOnClickListener(this);

        Intent intent = getIntent();
        phonenum = intent.getStringExtra("user");

        sync();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        String text=lv_history.getItemAtPosition(position)+"";
        Toast.makeText(this, "position="+position+" text="+text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.sync_his:
                sync();
                Toast.makeText(this, "同步成功", Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState){
        // TODO Auto-generated method stub
        switch(scrollState){
            case SCROLL_STATE_FLING:
                Log.i(TAG, "用户在手指离开屏幕之前，由于用力划了一下，试图仍依靠惯性继续滑动");
                break;
            case SCROLL_STATE_IDLE:
                Log.i(TAG, "视图已经停止滑动");
                break;
            case SCROLL_STATE_TOUCH_SCROLL:
                Log.i(TAG, "手指没有离开屏幕，视图正在滑动");
                break;
            default:
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int arg1, int arg2, int arg3){
        // TODO Auto-generated method stub
    }

    public void sync(){
        new Thread(){
            @Override
            public void run(){
                operationManager operationManager = new operationManager();
                dataList = operationManager.history(phonenum);

                int msg = 1;
                hand1.sendEmptyMessage(msg);
            }
        }.start();
    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler(){
        @Override
        public void handleMessage(Message msg){
            if(msg.what == 1){
                simp_adapter=new SimpleAdapter(HistoryActivity.this, dataList, R.layout.item, new String[]{"pickupcode","time"}, new int[]{R.id.np_id,R.id.np_time});
                lv_history.setAdapter(simp_adapter);
            }
        }
    };
}