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.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
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.MesProBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.mes.MesProActivity;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;
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 flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.disposables.Disposable;

/**
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class MesSupplierAddActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.rl_pro_code)
    RelativeLayout rlProCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_img_code)
    TextView tvImgCode;
    @BindView(R.id.tv_send_time)
    TextView tvSendTime;
    @BindView(R.id.rl_time)
    RelativeLayout rlTime;
    @BindView(R.id.tv_send_num)
    TextView tvSendNum;
    private Map<String, Object> param;
    private MesProBean.ListBean choosePro;
    private String from;

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

    @Override
    protected void initData() {
        from = getIntent().getStringExtra("from");
        param = WorkUtils.simpleParam();
        param.put("continuousCasterCode", getIntent().getStringExtra("continuousCasterCode"));
    }

    private void submit() {
        if (choosePro == null) {
            toast("请选择产品");
            return;
        }
        if (TextUtils.equals(from, "1")) {
            EventBus.getDefault().post(new AddMachineAddChildEvent(2, JSON.toJSONString(param)));
            finish();
        } else {
            RtfUtils.getMesRtf().supplierAdd("1", "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;
                    }
                    finish();
                    toast("添加成功");
                    EventBus.getDefault().post(new RefreshMachineEvent());
                }
            });
        }
    }

    @Override
    protected void initEvent() {
    }

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


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


    @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);
            tvCode.setText(choosePro.getProductCode());
            tvName.setText(choosePro.getProductName());
            tvImgCode.setText(choosePro.getDrawingNumber());
            param.put("productsCode", choosePro.getProductCode());
            param.put("productsName", choosePro.getProductName());
            param.put("deliveryNumber", choosePro.getDeliveryNumber());
            param.put("figureNumber", choosePro.getDrawingNumber());

        }
    }

    @OnClick({R.id.rl_pro_code, R.id.rl_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_pro_code:
                startActivityForResult(MesProActivity.class, 10001
                        , "from", "1");
                break;
            case R.id.rl_time:
                PickTimeUtils.showPickThree(this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tvSendTime.setText(TimeUtils.date2String(date, new SimpleDateFormat("yyyy-MM-dd")));
                        param.put("deliveryDateTime", TimeUtils.date2Millis(date) / 1000);
                    }
                }).show();
                break;
        }
    }
}
