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.EditText;
import android.widget.LinearLayout;
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.AddMachineAddChildEvent;
import com.wd.workoffice.bean.event.RefreshMachineEvent;
import com.wd.workoffice.bean.mesBean.MachineDetailBean;
import com.wd.workoffice.bean.mesBean.MesClientBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;

import org.greenrobot.eventbus.EventBus;

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;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesSpeedAddActivity extends WorkToolBarActivity {


    @BindView(R.id.tv_steel_type)
    EditText tvSteelType;
    @BindView(R.id.tv_speed)
    EditText tvSpeed;
    @BindView(R.id.rl_cover)
    RelativeLayout rlCover;
    @BindView(R.id.tv_cover1)
    EditText tvCover1;
    @BindView(R.id.tv_cover2)
    EditText tvCover2;
    @BindView(R.id.tv_cover3)
    EditText tvCover3;
    @BindView(R.id.tv_cover4)
    TextView tvCover4;
    @BindView(R.id.rl_cover4)
    RelativeLayout rlCover4;
    @BindView(R.id.ll_cover)
    LinearLayout llCover;
    @BindView(R.id.rl_protection)
    RelativeLayout rlProtection;
    @BindView(R.id.tv_protection1)
    EditText tvProtection1;
    @BindView(R.id.tv_protection2)
    EditText tvProtection2;
    @BindView(R.id.tv_protection3)
    EditText tvProtection3;
    @BindView(R.id.tv_protection4)
    TextView tvProtection4;
    @BindView(R.id.rl_protection4)
    RelativeLayout rlProtection4;
    @BindView(R.id.ll_protection)
    LinearLayout llProtection;
    //    private Map<String, Object> param;
    private String from;
    private MachineDetailBean.SpeedsBean machineDetailBean;

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

    @Override
    protected void initData() {
        from = getIntent().getStringExtra("from");
        machineDetailBean = new MachineDetailBean.SpeedsBean();
        machineDetailBean.setContinuousCasterCode(getIntent().getStringExtra("continuousCasterCode"));
    }


    @Override
    protected void initEvent() {
    }

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


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

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

    private void submit() {
        String speed = tvSpeed.getText().toString();
        String steelType = tvSteelType.getText().toString();
        if (TextUtils.isEmpty(speed)) {
            toast("请输入拉速");
            return;
        }
        if (TextUtils.isEmpty(steelType)) {
            toast("请输入钢种");
            return;
        }
        machineDetailBean.setCastingSpeed(speed);
        machineDetailBean.setSteelGrade(steelType );


        MachineDetailBean.SpeedsBean.CovAgentBean covAgent = machineDetailBean.getCovAgent();
        if (covAgent == null) {
            covAgent = new MachineDetailBean.SpeedsBean.CovAgentBean();
            machineDetailBean.setCovAgent(covAgent);
        }
        covAgent.setLadleCoveringAgentName(tvCover1.getText().toString());
        covAgent.setAcidAlkaliDegreeCov(tvCover2.getText().toString());
        covAgent.setChemicalCompositionCov(tvCover3.getText().toString());

        MachineDetailBean.SpeedsBean.MouldFluBean mouldFlu = machineDetailBean.getMouldFlu();
        if (mouldFlu == null) {
            mouldFlu = new MachineDetailBean.SpeedsBean.MouldFluBean();

            machineDetailBean.setMouldFlu(mouldFlu);
        }
        mouldFlu.setMouldFluxesName(tvProtection1.getText().toString());
        mouldFlu.setAcidAlkaliDegreeFlu(tvProtection2.getText().toString());
        mouldFlu.setChemicalCompositionFlu(tvProtection3.getText().toString());

        covAgent.setSupplierNameCov(tvCover4.getText().toString());
        mouldFlu.setSupplierNameFlu(tvProtection4.getText().toString());

        if (TextUtils.equals(from, "1")) {
            EventBus.getDefault().post(new AddMachineAddChildEvent(7, JSON.toJSONString(machineDetailBean)));
            finish();
        } else {
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(machineDetailBean));
            RtfUtils.getMesRtf().speedAdd("1", "1", requestBody).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);
        switch (requestCode) {
            case 10001:
                if (resultCode == 10001) {
                    MesClientBean.ListBean modifyClient = JSON.parseObject(data.getStringExtra("data"), MesClientBean.ListBean.class);
                    MachineDetailBean.SpeedsBean.CovAgentBean covAgent = machineDetailBean.getCovAgent();
                    if (covAgent == null) {
                        covAgent = new MachineDetailBean.SpeedsBean.CovAgentBean();
                        machineDetailBean.setCovAgent(covAgent);
                    }
                    tvCover4.setText(modifyClient.getCustomerName());
                    covAgent.setSupplierCodeCov(modifyClient.getCustomerCode());
                    covAgent.setSupplierNameCov(modifyClient.getCustomerName());
                }
                break;
            case 10002:
                if (resultCode == 10001) {
                    MesClientBean.ListBean modifyClient = JSON.parseObject(data.getStringExtra("data"), MesClientBean.ListBean.class);
                    MachineDetailBean.SpeedsBean.MouldFluBean mouldFlu = machineDetailBean.getMouldFlu();
                    if (mouldFlu == null) {
                        mouldFlu = new MachineDetailBean.SpeedsBean.MouldFluBean();
                        machineDetailBean.setMouldFlu(mouldFlu);
                    }
                    tvProtection4.setText(modifyClient.getCustomerName());
                    mouldFlu.setSupplierCodeFlu(modifyClient.getCustomerCode());
                    mouldFlu.setSupplierNameFlu(modifyClient.getCustomerName());
                }
                break;
        }
    }


//    @OnClick({R.id.rl_cover4, R.id.rl_protection4})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.rl_cover4:
//                startActivityForResult(MesChooseClientActivity.class,
//                        10001, "from", "1");
//                break;
//            case R.id.rl_protection4:
//                startActivityForResult(MesChooseClientActivity.class,
//                        10002, "from", "1");
//                break;
//        }
//    }
}
