package com.ewei.demo.otherui;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.ewei.demo.R;
import com.ewei.demo.otherui.adapter.LocationPoiInfoListAdapter;
import com.ewei.sdk.base.BaseActivity;
import com.ewei.sdk.constants.NetWorkValue;
import com.ewei.sdk.utility.LogUtils;
import com.ewei.sdk.widget.ClearEditText;
import com.ewei.sdk.widget.NetWorkList;

import java.util.List;


/**
 * Created by Administrator on 2016/5/31.
 */
public class LocationSearchActivity extends BaseActivity implements View.OnClickListener, OnGetPoiSearchResultListener, TextWatcher, OnItemClickListener, NetWorkList.OnLoadListener {

    private static final String TAG = LocationSearchActivity.class.getSimpleName();
    private LinearLayout mLLBack;
    private ClearEditText mEtSearch;

    private LocationPoiInfoListAdapter mLocationPoiInfoListAdapter;
    private NetWorkList mListView;

    private String mCity;
    private int mPage = 1;

    private PoiSearch mPoiSearch;

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

    private void initControl() {
        mCity = getIntent().getStringExtra("city");
        mLLBack = (LinearLayout) findViewById(R.id.ll_cmn_bs_back);
        mLLBack.setOnClickListener(this);
        mEtSearch = (ClearEditText) findViewById(R.id.cet_search_key);
        mEtSearch.addTextChangedListener(this);

        mLocationPoiInfoListAdapter = new LocationPoiInfoListAdapter(this, null);
        mListView = (NetWorkList) findViewById(R.id.lv_search_bdmap_result);
        mListView.setAdapter(mLocationPoiInfoListAdapter);
        mListView.setOnItemClickListener(this);
        mListView.setOnLoadListener(this);
        mListView.showNoData(NetWorkValue.NETWORK_NODATA_TICKET_NOLOADING, "请搜索相关地址");

        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_cmn_bs_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void searchCityKey() {
        String key = mEtSearch.getText().toString();
        if (TextUtils.isEmpty(key)) {
            return;
        }
        showLoadingDialog("");
        try {
            mPoiSearch.searchInCity((new PoiCitySearchOption())
                    .city(mCity)
                    .keyword(key)
                    .pageNum(mPage));
        } catch (RuntimeException e) {
            LogUtils.i(TAG, e.getMessage());
        }
    }

    @Override
    public void onGetPoiResult(PoiResult poiResult) {
        hideLoadingDialog();
        hideSoftKeyboard(mEtSearch);
        mListView.stopLoad();
        List<PoiInfo> poiInfos = poiResult.getAllPoi();
        if (poiInfos != null && poiInfos.size() > 0) {
            mListView.hideNetWork();
            if (mPage > 1) {
                mLocationPoiInfoListAdapter.addMoreData(poiInfos);
            } else {
                mLocationPoiInfoListAdapter.removeAllData();
                mLocationPoiInfoListAdapter.addMoreData(poiInfos);
            }
        } else {
            if (mPage > 1) {
                mListView.setPullLoadEnable(false);
            } else {
                mListView.showNoData(NetWorkValue.NETWORK_NODATA_TICKET_NOLOADING, "未找到相关地址");
            }
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
        hideLoadingDialog();
    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
        hideLoadingDialog();
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String key = mEtSearch.getText().toString();
        if (!TextUtils.isEmpty(key)) {
            onRefresh();
        } else {
            mLocationPoiInfoListAdapter.removeAllData();
            mLocationPoiInfoListAdapter.upDateData();
            mListView.showNoData(NetWorkValue.NETWORK_NODATA_TICKET_NOLOADING, "请搜索相关地址");
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PoiInfo pi = (PoiInfo) parent.getAdapter().getItem(position);
        Bundle bundle = new Bundle();
        bundle.putParcelable("poiinfo", pi);
        Intent intent = getIntent();
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onLoadMore() {
        searchCityKey();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        searchCityKey();
    }
}
