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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.AddMachineEvent;
import com.wd.workoffice.bean.event.AddUserDataEvent;
import com.wd.workoffice.bean.mesBean.MachineBean;
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.UserDataChooseMachineAdapter;
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 MesChooseMachineActivity extends WorkToolBarActivity {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.rv_client)
    RecyclerView rvClient;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    private Map<String, Object> param;
    private List<MachineBean> clientList;
    private UserDataChooseMachineAdapter clientAdapter;
    private UserDataBean.ListBean data;
    private Map<String, Object> paramMap;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        llSearch.setVisibility(View.GONE);
        rvClient.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        etKey.setHint("搜索连铸机");
    }

    @Override
    protected void initData() {
        data = JSON.parseObject(getIntent().getStringExtra("data"), UserDataBean.ListBean.class);
        clientList = new ArrayList<>();
        clientAdapter = new UserDataChooseMachineAdapter(R.layout.item_store_client, clientList);
        clientAdapter.bindToRecyclerView(rvClient);
        clientAdapter.setEmptyView(R.layout.view_empty_content, rvClient);
        paramMap = WorkUtils.simpleParam();
        param = WorkUtils.simpleParam();
        param.put("paramMap", paramMap);
        param.put("steelmakingWorksCode", data.getSteelmakingWorksCode());
        paramMap.put("steelmakingWorksCode", data.getSteelmakingWorksCode());
        getData();
    }

    private void getData() {
        RtfUtils.getMesRtf().machineList("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;
                }
                List<MachineBean> getList = JSON.parseArray(data.getData().toString(), MachineBean.class);
                clientList.clear();
                clientList.addAll(getList);
                clientAdapter.notifyDataSetChanged();
                toast("搜索成功");
            }
        });
    }

    @Override
    protected void initEvent() {
        clientAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (TextUtils.equals("1", getIntent().getStringExtra("from"))) {// 驳回编辑
                    Intent intent = new Intent();
                    intent.putExtra("data", JSON.toJSONString(clientList.get(position)));
                    setResult(10003, intent);
                    finish();
                } else {
                    data.setContinuousCasterName(clientList.get(position).getContinuousCasterName());
                    data.setContinuousCasterType(clientList.get(position).getContinuousCasterType());
                    data.setContinuousCasterCode(clientList.get(position).getContinuousCasterCode());
                    startActivity(MesChooseMachineDetailActivity.class, "data", JSON.toJSONString(data));
                }
            }
        });
    }

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


    @OnClick(R.id.btn_search)
    public void onViewClicked() {
        paramMap.put("continuousCasterName", etKey.getText().toString());
        getData();
    }

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

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

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

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

    @Subscribe
    public void refresh(AddMachineEvent event) {
        getData();
    }


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

}
