package site.ruiqiao.renrentou.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.ViewStatus;

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

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.mvp.XActivity;
import cn.droidlover.xdroidmvp.router.Router;
import site.ruiqiao.renrentou.R;
import site.ruiqiao.renrentou.model.AdCompany;
import site.ruiqiao.renrentou.present.AdCompanySearchPresent;

/**
 * @author ruiqiao
 * @project Renrentou
 * @package site.ruiqiao.renrentou.ui
 * @date 2017/9/6
 * @des
 */

public class AdCompanySearchActivity extends XActivity<AdCompanySearchPresent>{
    private static final String TAG = AdCompanySearchActivity.class.getSimpleName();
    @BindView(R.id.search_text) EditText searchEt;
    @BindView(R.id.ad_pull_to_refresh) PullToRefreshLayout pullToRefreshLayout;
    @BindView(R.id.ad_list) ListView listView;

    private String searchText = "";

    @Override
    public void initData(Bundle savedInstanceState) {
        setTopbar();
        initListView();
        initPullToRefreshLayout();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_ad_company_search;
    }

    @Override
    public AdCompanySearchPresent newP() {
        return new AdCompanySearchPresent();
    }

    private void setTopbar(){

    }

    @OnClick({
            R.id.title_left_layout,
            R.id.title_right_layout
    })

    public void clickEvent(View view) {
        switch (view.getId()) {
            case R.id.title_left_layout:
                Log.d(TAG, "clickEvent: topbar_left_btn");
                finish();
                break;
            case R.id.title_right_layout:
                Log.d(TAG, "clickEvent: topbar_search_btn");
                String searchText = searchEt.getText().toString();
                if(!TextUtils.isEmpty(searchText)){
                    search(searchText);
                }else{
                    showToast("请输入关键字");
                }
                break;
        }
    }

    public static void launch(Activity activity , int reqcode) {
        Router.newIntent(activity)
                .to(AdCompanySearchActivity.class)
                .data(new Bundle())
                .requestCode(reqcode)
                .launch();
    }

    private void search(String text){
//        searchEt.setText(text);
        searchText = text;
        //pullToRefreshLayout.setVisibility(View.VISIBLE);
        if(list.isEmpty()){
            pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
        }
        getP().refresh(text);
    }


    public final static int AD_COMPNAY_SELECTED = -1;
    private void initListView(){
        listView.setAdapter(adAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("name",list.get(i).getName());
                bundle.putString("id",list.get(i).getId());
                intent.putExtras(bundle);
                context.setResult(AD_COMPNAY_SELECTED,intent);
                context.finish();
            }
        });
    }

    private void initPullToRefreshLayout(){
        pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                if(list.isEmpty()){
                    pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
                }
                getP().refresh(searchText);
            }

            @Override
            public void loadMore() {
                getP().loadMore(searchText);
            }
        });

        pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getP().refresh();
            }
        }, 500);
    }



    private List<AdCompany> list = new ArrayList<>();
    private AdAdapter adAdapter = new AdAdapter();

    private class ViewHolder{
        TextView name;
        TextView address;
    }

    private class AdAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public AdCompany getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if(view == null){
                holder = new ViewHolder();
                view = getLayoutInflater().inflate(R.layout.item_ad_company, null);
                holder.name = (TextView) view.findViewById(R.id.name);
                holder.address = (TextView) view.findViewById(R.id.address);
                view.setTag(holder);
            }
            else{
                holder = (ViewHolder) view.getTag();
            }

            AdCompany adCompany = list.get(i);
            holder.name.setText(adCompany.getName());
            holder.address.setText(adCompany.getAddress());
            return view;
        }
    }


    public void netError(int type,String msg){
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.finishRefresh();
        showToast(msg);
        if(list.isEmpty()){
            pullToRefreshLayout.showView(ViewStatus.ERROR_STATUS);
            pullToRefreshLayout.setCanLoadMore(false);
            setErrorViewClickEvent();
        }
    }

    public void fail(String type,String msg){
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.finishRefresh();
        showToast(msg);
        if(list.isEmpty()){
            pullToRefreshLayout.showView(ViewStatus.ERROR_STATUS);
            pullToRefreshLayout.setCanLoadMore(false);
            setErrorViewClickEvent();
        }
    }

    public void dataIsEmpty(){
        pullToRefreshLayout.finishRefresh();
        pullToRefreshLayout.showView(ViewStatus.EMPTY_STATUS);
        pullToRefreshLayout.setCanLoadMore(false);
        pullToRefreshLayout.finishRefresh();
        setEmptyViewClickEvent();
    }

    public void dataIsNotMore(){
        showToast("没有更多数据");
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.setCanLoadMore(false);
        pullToRefreshLayout.showView(ViewStatus.CONTENT_STATUS);

    }

    public void refreshData(List<AdCompany> adCompanies){
        list.clear();
        list.addAll(adCompanies);
        adAdapter.notifyDataSetChanged();
        pullToRefreshLayout.showView(ViewStatus.CONTENT_STATUS);
        pullToRefreshLayout.setCanLoadMore(true);
        pullToRefreshLayout.finishRefresh();
    }

    public void loadedData(List<AdCompany> adCompanies){
        list.addAll(adCompanies);
        adAdapter.notifyDataSetChanged();
        pullToRefreshLayout.finishLoadMore();
    }

    private boolean isSetEmptyView = false;
    private void setEmptyViewClickEvent(){
        if(!isSetEmptyView){
            isSetEmptyView = true;
            pullToRefreshLayout.getView(ViewStatus.EMPTY_STATUS).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
//                    getP().refresh();
                }
            });
        }
    }
    private boolean isSetErrorView = false;
    private void setErrorViewClickEvent(){
        if(!isSetErrorView){
            isSetErrorView = true;
            pullToRefreshLayout.getView(ViewStatus.ERROR_STATUS).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    pullToRefreshLayout.showView(ViewStatus.LOADING_STATUS);
//                    getP().refresh();
                }
            });
        }
    }

}