package com.wd.workoffice.ui.activity.mes.user;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.AddUserDataEvent;
import com.wd.workoffice.bean.event.ModifyFactoryEvent;
import com.wd.workoffice.bean.event.ModifyMachineEvent;
import com.wd.workoffice.bean.mesBean.FactoryDetailBean;
import com.wd.workoffice.bean.mesBean.UserDataBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.adapter.UserDataAdd3Adapter;
import com.wd.workoffice.ui.adapter.UserDataAdd3LabelAdapter;
import com.wd.workoffice.ui.adapter.UserDataAdd3RefAdapter;
import com.wd.workoffice.util.WorkUtils;

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

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

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 MesUserDataAdd3Activity extends WorkToolBarActivity {
    @BindView(R.id.tv_factory)
    TextView tvFactory;
    @BindView(R.id.rl_client)
    RelativeLayout rlClient;
    @BindView(R.id.tv_sum)
    TextView tvSum;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.rv_data1)
    RecyclerView rvData1;
    @BindView(R.id.rv_data2)
    RecyclerView rvData2;
    @BindView(R.id.rv_data3)
    RecyclerView rvData3;
    @BindView(R.id.tv_machine)
    TextView tvMachine;
    @BindView(R.id.rl_machine)
    RelativeLayout rlMachine;
    private UserDataBean.ListBean data;
    private List<FactoryDetailBean.SteelFurnListBean> data1List;
    private List<FactoryDetailBean.SteelRefFurnListBean> data2List;
    private List<FactoryDetailBean.SteelLadleListBean> data3List;
    private UserDataAdd3Adapter data1Adapter;
    private UserDataAdd3RefAdapter data2Adapter;
    private UserDataAdd3LabelAdapter data3Adapter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvData1.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData2.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvData3.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }

    @Override
    protected void initData() {
        data = JSON.parseObject(getIntent().getStringExtra("data"), UserDataBean.ListBean.class);
        data1List = new ArrayList<>();
        data2List = new ArrayList<>();
        data3List = new ArrayList<>();
        data1Adapter = new UserDataAdd3Adapter(R.layout.item_user_add3, data1List);
        data2Adapter = new UserDataAdd3RefAdapter(R.layout.item_user_add3, data2List);
        data3Adapter = new UserDataAdd3LabelAdapter(R.layout.item_add3_label, data3List);
        data1Adapter.bindToRecyclerView(rvData1);
        data2Adapter.bindToRecyclerView(rvData2);
        data3Adapter.bindToRecyclerView(rvData3);
        getData();
    }

    private void getData() {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("steelmakingWorksCode", data.getSteelmakingWorksCode());
        RtfUtils.getMesRtf().factoryDetail("1", WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

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

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                FactoryDetailBean factoryDetailBean = JSON.parseObject(data.getData().toString(), FactoryDetailBean.class);
                FactoryDetailBean.SteelWorksBean steelWorks = factoryDetailBean.getSteelWorks();
                if (steelWorks != null) {
                    tvSum.setText(steelWorks.getAnnualOutput());
                    tvFactory.setText(steelWorks.getSteelmakingWorksName());
                    tvType.setText(steelWorks.getSteelGrade());
                }
                data1List.clear();
                data2List.clear();
                data3List.clear();
                if (factoryDetailBean.getSteelFurnList() != null) {
                    data1List.addAll(factoryDetailBean.getSteelFurnList());
                }
                if (factoryDetailBean.getSteelRefFurnList() != null) {
                    data2List.addAll(factoryDetailBean.getSteelRefFurnList());
                }
                if (factoryDetailBean.getSteelLadleList() != null) {
                    data3List.addAll(factoryDetailBean.getSteelLadleList());
                }
                data1Adapter.notifyDataSetChanged();
                data2Adapter.notifyDataSetChanged();
                data3Adapter.notifyDataSetChanged();

            }
        });
    }

    @Override
    protected void initEvent() {
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_modify, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.modify:
                startActivity(MesChooseFactoryModifyActivity.class,
                        "data",JSON.toJSONString(data));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.rl_machine)
    public void onViewClicked() {
        startActivity(MesChooseMachineActivity.class,"data",JSON.toJSONString(data));
    }
    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe
    public void refresh(AddUserDataEvent event) {
        finish();
    }
    @Subscribe
    public void refresh(ModifyFactoryEvent event) {
        getData();
    }


}
