package cn.dankal.yunlaboratory.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.api.BaseApi;
import cn.dankal.basiclib.api.UserService;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.mvp.BasePresenter;
import cn.dankal.basiclib.model.BaseModel;
import cn.dankal.basiclib.model.ErrCodeModel;
import cn.dankal.basiclib.model.SampleRecordBody;
import cn.dankal.basiclib.model.SubmitResultBody;
import cn.dankal.basiclib.rx.MapFunc;
import cn.dankal.basiclib.rx.NormalSubscriber;
import cn.dankal.basiclib.util.TimeUtil;
import cn.dankal.yunlaboratory.R;
import cn.dankal.yunlaboratory.ui.widget.MainButton;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UploadResultActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_result)
    EditText etResult;
    @BindView(R.id.btn_upload)
    MainButton btnUpload;

    private SubmitResultBody body;

    private SampleRecordBody sampleRecordBody;

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_upload_result;
    }

    @Override
    protected void initComponents() {
        tvTitle.setText("结果录入");
        body = (SubmitResultBody) getIntent().getSerializableExtra("data");

        if (getIntent().hasExtra("data2")) {
            sampleRecordBody = (SampleRecordBody) getIntent().getSerializableExtra("data2");
        }

        btnUpload.setEnabled(true);
    }


    @OnClick({R.id.iv_onback, R.id.btn_upload})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_onback:
                finish();
                break;
            case R.id.btn_upload:
                String report = etResult.getText().toString();

                if (!TextUtils.isEmpty(report)) {
                    body.setContent(report);

                    UserServiceFactory.submitReport(body).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(BaseModel stringBaseModel) {
                            if (sampleRecordBody != null) {

                                sampleRecordBody.setTest_time(TimeUtil.getTime(System.currentTimeMillis() / 1000, TimeUtil.DATEFORMATER));
                                BaseApi.getRetrofit(BaseApi.OTHER_BASE_URL).create(UserService.class).sendsamplerecord(sampleRecordBody)
                                        .observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new NormalSubscriber<String>() {
                                    @Override
                                    public void onSubscribe(Disposable d) {

                                    }

                                    @Override
                                    public void onNext(String s) {
                                        ErrCodeModel model = JSON.parseObject(s, ErrCodeModel.class);

                                        if (model.getErrcode() == 0) {
                                            showToast("上传成功");
                                            view.postDelayed(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Intent intent = new Intent(UploadResultActivity.this, MainActivity.class);
                                                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                                    startActivity(intent);
                                                    finish();
                                                }
                                            }, 1000);
                                        } else {
                                            showToast(model.getErrmsg());
                                        }
                                    }
                                });

                            } else {
                                showToast("上传成功");
                                view.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        finish();
                                    }
                                }, 1000);
                            }

                        }
                    });


//
                } else {
                    showToast("请填写样本检测报告");
                }
                break;
        }
    }
}
