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.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

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

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

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

    private String TAG = "NoPickActivity";
    private ListView lv_nopick;
    private ArrayAdapter<String> arr_adapter;
    private SimpleAdapter simp_adapter;
    private String phonenum;
    private List<Map<String, Object>> dataList;
    private ImageButton sync_np;

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

        dataList=new ArrayList<Map<String,Object>>();
        lv_nopick = (ListView) findViewById(R.id.lv_nopick);
        sync_np = (ImageButton) findViewById(R.id.sync_np);

        simp_adapter=new SimpleAdapter(this, dataList, R.layout.item, new String[]{"id","time"}, new int[]{R.id.np_id,R.id.np_time});
        lv_nopick.setAdapter(simp_adapter);
        lv_nopick.setOnItemClickListener(this);
        lv_nopick.setOnScrollListener(this);
        sync_np.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_nopick.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_np:
                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(){
                goodManager goodManager = new goodManager();
                dataList = goodManager.nopick(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(NoPickActivity.this, dataList, R.layout.item, new String[]{"id","time"}, new int[]{R.id.np_id,R.id.np_time});
                lv_nopick.setAdapter(simp_adapter);
            }
        }
    };


}