package cn.dankal.yunlaboratory.ui.activity;

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

import java.util.List;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
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.ProjectModel;
import cn.dankal.basiclib.model.SampleRecordBody;
import cn.dankal.basiclib.model.SubmitRecordModel;
import cn.dankal.basiclib.model.SubmitResultBody;
import cn.dankal.basiclib.rx.MapFunc;
import cn.dankal.basiclib.rx.NormalSubscriber;
import cn.dankal.yunlaboratory.R;
import cn.dankal.yunlaboratory.ui.dialog.ChooseItemDialog;
import cn.dankal.yunlaboratory.ui.widget.MainButton;
import io.reactivex.disposables.Disposable;

public class StartTestActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_project_name)
    TextView tvProjectName;
    @BindView(R.id.tv_machine_name)
    TextView tvMachineName;
    @BindView(R.id.btn_next)
    MainButton btnNext;

    private ChooseItemDialog chooseProjectDialog, chooseDeviceDialog;
    private SubmitRecordModel recordModel;

    private List<ProjectModel> projectModels;
    private List<ProjectModel> deviceModels;

    private int projectId=-1;
    private int deviceId=-1;

    private SubmitResultBody submitResultBody;
    private SampleRecordBody sampleRecordBody;
    @Override
    public BasePresenter createPresenter() {
        return null;
    }

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

    @Override
    protected void initComponents() {
        tvTitle.setText("开始试验");
        btnNext.setEnabled(true);
        recordModel = (SubmitRecordModel) getIntent().getSerializableExtra("data");
        chooseProjectDialog = new ChooseItemDialog(this);
        chooseDeviceDialog = new ChooseItemDialog(this);
        submitResultBody=new SubmitResultBody();
        submitResultBody.setUpload_list_id(recordModel.getId());

        getProjectList();
        getDeviceList();

        sampleRecordBody=new SampleRecordBody();
        if (!TextUtils.isEmpty(recordModel.getSample_num())){

            sampleRecordBody.setSample_num(recordModel.getSample_num());
            sampleRecordBody.setSample_name(recordModel.getSample_name());
            sampleRecordBody.setSend_user(recordModel.getSend_user());
            sampleRecordBody.setGet_user(recordModel.getGet_user());
            sampleRecordBody.setGet_time(recordModel.getCreated_at());
            sampleRecordBody.setTest_user(DKUserManager.getUserInfo().getName());
        }

        chooseProjectDialog.setViewClick(R.id.tv_positive, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseProjectDialog.dismiss();
                int pos = chooseProjectDialog.getCurrentItem();

                if (projectModels!=null&&pos<projectModels.size()){
                    tvProjectName.setText(projectModels.get(pos).getName());
                    projectId=projectModels.get(pos).getId();
                    sampleRecordBody.setProject_name(projectModels.get(pos).getName());
                }
            }
        });

        chooseDeviceDialog.setViewClick(R.id.tv_positive, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseDeviceDialog.dismiss();
                int pos = chooseDeviceDialog.getCurrentItem();

                if (deviceModels!=null&&pos<deviceModels.size()){

                    sampleRecordBody.setDevice_name(deviceModels.get(pos).getName());
                    sampleRecordBody.setDevice_num(String.valueOf(deviceModels.get(pos).getId()));

                    tvMachineName.setText(deviceModels.get(pos).getName());
                    deviceId=deviceModels.get(pos).getId();
                }
            }
        });
    }

    @OnClick({R.id.iv_onback, R.id.card_choose_project, R.id.card_choose_machine, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_onback:
                finish();
                break;
            case R.id.card_choose_project:
                chooseProjectDialog.show();
                break;
            case R.id.card_choose_machine:
                chooseDeviceDialog.show();
                break;
            case R.id.btn_next:
                if (projectId==-1){
                    showToast("请选择项目");
                    return;
                }

                if (deviceId==-1){
                    showToast("请选择仪器");
                    return;
                }
                submitResultBody.setProject_id(projectId);
                submitResultBody.setDevice_id(deviceId);
                Intent intent = new Intent(this, UploadResultActivity.class);
                intent.putExtra("data",submitResultBody);
                if (!TextUtils.isEmpty(recordModel.getSample_num())){
                    intent.putExtra("data2",sampleRecordBody);
                }
                startActivity(intent);
                finish();
                break;
        }
    }

    public void getProjectList() {
        UserServiceFactory.getProjectList(recordModel.getSample_num()).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel<List<ProjectModel>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseModel<List<ProjectModel>> listBaseModel) {

                if (listBaseModel.getData() != null && listBaseModel.getData().size() > 0) {
                    projectModels = listBaseModel.getData();
                    String[] titles = new String[projectModels.size()];

                    for (int i = 0; i < projectModels.size(); i++) {
                        ProjectModel projectModel = projectModels.get(i);

                        titles[i] = projectModel.getName();
                    }

                    chooseProjectDialog.setData(titles);
                }
            }
        });
    }

    public void getDeviceList() {
        UserServiceFactory.getDeviceList(recordModel.getSample_num()).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel<List<ProjectModel>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseModel<List<ProjectModel>> listBaseModel) {
                if (listBaseModel.getData() != null && listBaseModel.getData().size() > 0) {
                    deviceModels = listBaseModel.getData();
                    String[] titles = new String[deviceModels.size()];

                    for (int i = 0; i < deviceModels.size(); i++) {
                        ProjectModel projectModel = deviceModels.get(i);

                        titles[i] = projectModel.getName();
                    }

                    chooseDeviceDialog.setData(titles);
                }
            }
        });
    }
}
