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.AddMachineModifyChildEvent;
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 MesSpeedModifyActivity 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 String from;
    private MachineDetailBean.SpeedsBean machineDetailBean;

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

    @Override
    protected void initData() {
        from = getIntent().getStringExtra("from");
        machineDetailBean = JSON.parseObject(getIntent().getStringExtra("data"), MachineDetailBean.SpeedsBean.class);
        MachineDetailBean.SpeedsBean.CovAgentBean covAgent = machineDetailBean.getCovAgent();
        if (covAgent != null) {
            tvCover1.setText(covAgent.getLadleCoveringAgentName());
            tvCover2.setText(covAgent.getAcidAlkaliDegreeCov());
            tvCover3.setText(covAgent.getChemicalCompositionCov());
            tvCover4.setText(covAgent.getSupplierNameCov());
        }
        MachineDetailBean.SpeedsBean.MouldFluBean mouldFlu = machineDetailBean.getMouldFlu();
        if (mouldFlu != null) {
            tvProtection1.setText(mouldFlu.getMouldFluxesName());
            tvProtection2.setText(mouldFlu.getAcidAlkaliDegreeFlu());
            tvProtection3.setText(mouldFlu.getChemicalCompositionFlu());
            tvProtection4.setText(mouldFlu.getSupplierNameFlu());
        }
        tvSpeed.setText(machineDetailBean.getCastingSpeed());
        tvSteelType.setText(machineDetailBean.getSteelGrade());
    }


    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);

        if (TextUtils.equals(from, "1")) {
            EventBus.getDefault().post(new AddMachineModifyChildEvent(7, JSON.toJSONString(machineDetailBean)
                    , Integer.valueOf(getIntent().getStringExtra("position"))));
            finish();
            return;
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(machineDetailBean));
        RtfUtils.getMesRtf().speedAdd("1", "2", 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());
            }
        });
    }
    private void delete() {
        RtfUtils.getMesRtf().speedDelete(machineDetailBean.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 initEvent() {
    }

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


    @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);
    }

    @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;
        }
    }
}
