package com.android.chmo.ui.activity.model;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.base.BaseActivity;
import com.android.chmo.constant.Constants;
import com.android.chmo.http.HttpApi;
import com.android.chmo.http.RequestCallback;
import com.android.chmo.http.response.ModelListRes;
import com.android.chmo.http.service.ModelService;
import com.android.chmo.model.BrokerInfo;
import com.android.chmo.model.ModelInfo;
import com.android.chmo.ui.adpater.ModelAdapter;
import com.android.chmo.ui.view.EmptyView;
import com.android.chmo.ui.view.RefreshListView;
import com.android.chmo.ui.view.TopBar;
import com.android.chmo.utils.XUtilsImage;
import com.google.gson.Gson;

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

import butterknife.BindView;


public class BrokerModelActivity extends BaseActivity implements RefreshListView.RefreshListener{
    @BindView(R.id.topBar)
    TopBar topBar;
    @BindView(R.id.empty)
    EmptyView emptyView;
    @BindView(R.id.refreshList)
    RefreshListView refreshListView;

    private TextView descView;
    private TextView modelCountView;
    private ImageView headView;

    private ModelAdapter modelAdapter;

    private BrokerInfo broker;

    private int pageNo = 1;

    private List<ModelInfo> mList = new ArrayList<>();

    @Override
    protected int getContentResId() {
        return R.layout.activity_broker_model;
    }


    @Override
    public void initView() {
        broker = (BrokerInfo)getIntent().getSerializableExtra("broker");

        topBar.setTitle(broker.name);

        refreshListView.setRefreshListener(this);
        refreshListView.getListView().addHeaderView(initHeaderView());
        modelAdapter = new ModelAdapter(this);
        modelAdapter.setType(2);
        refreshListView.setAdapter(modelAdapter);

        refreshListView.autoRefresh();


    }

    private View initHeaderView(){
       View view = LayoutInflater.from(this).inflate(R.layout.broker_info_layout,null);
       headView = (ImageView)view.findViewById(R.id.head);
       descView = (TextView) view.findViewById(R.id.desc);
       modelCountView = (TextView) view.findViewById(R.id.model_count);

        descView.setText(broker.intro);
        modelCountView.setText("模特" + broker.modelcount + "人");
        XUtilsImage.display(headView, HttpApi.getImgUrl(broker.logo),R.mipmap.def_img3);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), BrokerInfoActivity.class);
                intent.putExtra("broker", broker);
                openPage(intent);
            }
        });
       return view;
    }

    @Override
    public void onRefresh() {
        loadModel(1);
    }

    @Override
    public void onLoadMore() {
        refreshListView.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadModel(pageNo + 1);
            }
        }, 500);
    }

    private void loadModel(final int page) {
        ModelService.getBrokerModels(broker.brokepk, page, new RequestCallback() {
            @Override
            public void onSuccess(String result) {
                refreshListView.finishRefresh();
                ModelListRes res = new Gson().fromJson(result, ModelListRes.class);
                if(res.data != null) {
                    if(page == 1) {
                        mList.clear();
                    }
                    mList.addAll(res.data);
                    pageNo = page;
                    modelAdapter.setList(mList);

                    if(res.data.size() == Constants.PAGE_SIZE) {
                        refreshListView.setEnableLoadMore(true);
                    }else {
                        refreshListView.setNoMoreData();
                    }
                }
                emptyView.setVisibility(mList.size() ==0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onFailure(String error) {
                refreshListView.finishRefresh();
            }
        });
    }
}
