package com.taikor.investment.modules;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;

import com.google.gson.Gson;
import com.taikor.investment.R;
import com.taikor.investment.adapter.CustomerAdapter;
import com.taikor.investment.base.BaseFragment;
import com.taikor.investment.bean.Customer;
import com.taikor.investment.event.BooleanEvent;
import com.taikor.investment.modules.customer.AddCustomerActivity;
import com.taikor.investment.modules.customer.CustomerDescActivity;
import com.taikor.investment.mylibrary.TAnalytics;
import com.taikor.investment.utils.Constant_URL;
import com.taikor.investment.view.SimpleListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 客户
 * Created by Any on 2017/3/29.
 */

public class CustomerFragment extends BaseFragment implements
        AdapterView.OnItemClickListener, SimpleListView.OnLoadListener {

    @BindView(R.id.ib_add)
    ImageButton ibAdd;//添加客户
    @BindView(R.id.slv_customer)
    SimpleListView simpleListView;

    private int mPage = 1;
    private CustomerAdapter mAdapter = null;
    private ProgressDialog progressDialog;
    private List<Customer.CustomersBean> customers = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_customer;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mAdapter = new CustomerAdapter(getActivity());
        simpleListView.setAdapter(mAdapter);
        simpleListView.setOnItemClickListener(this);
        simpleListView.setOnLoadListener(this);

        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage(getString(R.string.progress_hint));
        progressDialog.setCancelable(true);

    }

    @Override
    public void initData() {
        getCustomerData(Constant_URL.CUSTOMER, mPage);
    }

    //获取投顾的客人列表
    public void getCustomerData(String url, final int page) {
        progressDialog.show();

        OkHttpUtils.get().url(url)
                .addParams("userID", "20000")//目前没有count,skip参数
                .build().execute(new Callback<Customer>() {
            @Override
            public Customer parseNetworkResponse(Response response, int id) throws Exception {
                Gson gson = new Gson();
                return gson.fromJson(response.body().string(), Customer.class);
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(Customer response, int id) {
                progressDialog.dismiss();
                if (response == null) {
                    return;
                }
                customers = response.getCustomers();
                mAdapter.setData(customers, true);
                simpleListView.finishLoad(true);
            }
        });
    }

    /**
     * 加载更多
     *
     * @param isRefresh true为下拉刷新 false为加载更多
     */
    @Override
    public void onLoad(boolean isRefresh) {
        if (isRefresh) {
            mPage = 1;
        } else {
            mPage++;
        }
        getCustomerData(Constant_URL.CUSTOMER, mPage);
    }

    //刷新数据
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void process(BooleanEvent event) {
        String type = event.getType();
        if (event.isFlag()) {
            if (type.equals("customer") || type.equals("all"))
                getCustomerData(Constant_URL.CUSTOMER, 1);
        }
    }

    @OnClick(R.id.ib_add)
    public void click() {
        Intent intent = new Intent(getActivity(), AddCustomerActivity.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), CustomerDescActivity.class);
        intent.putExtra("customerId", customers.get(position).getCustomerID());
        intent.putExtra("itemId", customers.get(position).getPortfolioID());
        startActivity(intent);
    }

    // 注册事件总线
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    // 解除事件总线
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //埋点
    @Override
    public void onHiddenChanged(boolean hidden) {
        if(!hidden){
            TAnalytics.onPageStart("customerPage");
        }else{
            TAnalytics.onPageLoad("customerPage","","","");
        }
    }
}
