package com.wd.workoffice.ui.activity.bat.work;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ContactsBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.WorkContactsInfoAdapter;
import com.wd.workoffice.util.MathUtils;

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

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 工作台 - 往来 - 详情 - 信息
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkContactsInfoActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_company_name)
    TextView tvCompanyName;
    @BindView(R.id.tv_link_man)
    TextView tvLinkMan;
    @BindView(R.id.tv_link_phone)
    TextView tvLinkPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_process)
    TextView tvProcess;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    private ContactsBean dataBean;
    private WorkContactsInfoAdapter workContactsInfoAdapter;
    private List<ContactsBean.PartTypeRelListBean> partTypeRelList =new ArrayList<>();

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvData.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        workContactsInfoAdapter = new WorkContactsInfoAdapter(R.layout.item_work_contacts_info,partTypeRelList);
        workContactsInfoAdapter.bindToRecyclerView(rvData);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        RtfUtils.getRtf().contactInfo(getIntent().getStringExtra("id")).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {


            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                dataBean = JSON.parseObject(data.getData().toString(), ContactsBean.class);
                tvCompanyName.setText(dataBean.getName());
                tvLinkMan.setText(dataBean.getContacter());
                tvLinkPhone.setText(dataBean.getPhone());
                tvAddress.setText(dataBean.getAddress());
                tvProcess.setText(getProcessString(dataBean.getProcessList()));
                partTypeRelList.addAll(dataBean.getPartTypeRelList());
                workContactsInfoAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initEvent() {
    }

    protected String getProcessString(List<ContactsBean.ProcessListBean> data) {
        String returnData = "";
        if (data != null && data.size() != 0) {
            for (ContactsBean.ProcessListBean process : data) {
                returnData = returnData + process.getName() + ",";
            }
            returnData = returnData.substring(0, returnData.length() - 1);
        } else {
            returnData = "无";
        }
        return returnData;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_work_contacts_info;
    }


}
