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

import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
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.AddMachineModifyChildEvent;
import com.wd.workoffice.bean.event.RefreshMachineEvent;
import com.wd.workoffice.bean.mesBean.MachineDetailBean;
import com.wd.workoffice.bean.mesBean.MesProBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.mes.ticket.MesTicketProActivity;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import androidx.annotation.Nullable;
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 MesStockModifyActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_pro)
    TextView tvPro;
    @BindView(R.id.rl_pro)
    RelativeLayout rlPro;
    @BindView(R.id.et_img_code)
    TextView etImgCode;
    @BindView(R.id.tv_stock)
    TextView tvStock;
    @BindView(R.id.et_content)
    EditText etContent;
    private Map<String, Object> param;
    private MesProBean.ListBean choosePro;
    private MachineDetailBean.StockListBean dataBean;
    private String from;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        from = getIntent().getStringExtra("from");
        dataBean = JSON.parseObject(getIntent().getStringExtra("data"), MachineDetailBean.StockListBean.class);
        param = WorkUtils.simpleParam();
        param.put("continuousCasterCode", dataBean.getContinuousCasterCode());
        param.put("id", dataBean.getId());
        if (!TextUtils.isEmpty(dataBean.getProductsCode())) {
            choosePro = new MesProBean.ListBean();
            choosePro.setProductCode(dataBean.getProductsCode());
            choosePro.setDrawingNumber(dataBean.getFigureNumber());
            choosePro.setProductName(dataBean.getProductsName());
//          param.put("stockNumber", choosePro.gets);
            tvPro.setText(choosePro.getProductName());
            etImgCode.setText(choosePro.getDrawingNumber());
        }
        etContent.setText(dataBean.getOtherInformationExplain());
    }


    @Override
    protected void initEvent() {
    }

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (TextUtils.equals(from, "1")) {
            getMenuInflater().inflate(R.menu.menu_finish, menu);
        }else{
            getMenuInflater().inflate(R.menu.menu_finish_delete, menu);

        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.finish:
                submit();
                break;
            case R.id.delete:
                delete();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void submit() {
        if (choosePro == null) {
            toast("请选择产品");
            return;
        }
        String content = etContent.getText().toString();
        param.put("otherInformationExplain", content);
        param.put("productsCode", choosePro.getProductCode());
        param.put("productsName", choosePro.getProductName());
        param.put("figureNumber", choosePro.getDrawingNumber());
//        param.put("stockNumber", choosePro.gets);

        if (TextUtils.equals(from, "1")) {
            EventBus.getDefault().post(new AddMachineModifyChildEvent(5, JSON.toJSONString(param)
                    , Integer.valueOf(getIntent().getStringExtra("position"))));
            finish();
            return;
        }
        RtfUtils.getMesRtf().stockAdd("1", "2", 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;
                }
                finish();
                toast("添加成功");
                EventBus.getDefault().post(new RefreshMachineEvent());
            }
        });
    }

    private void delete() {
        RtfUtils.getMesRtf().stockDelete(dataBean.getId(), "1").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;
                }
                finish();
                toast("刪除成功");
                EventBus.getDefault().post(new RefreshMachineEvent());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001 && resultCode == 10001) {
            choosePro = JSON.parseObject(data.getStringExtra("data"), MesProBean.ListBean.class);
            tvPro.setText(choosePro.getProductName());
            etImgCode.setText(choosePro.getDrawingNumber());
//            tvStock.setText(choosePro.get);
        }
    }

    @OnClick(R.id.rl_pro)
    public void onViewClicked() {
        startActivityForResult(MesUserProActivity.class, 10001, "id", getIntent().getStringExtra("id"));
    }
}
