package cn.dankal.yunlaboratory.ui.fragment;

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

import androidx.cardview.widget.CardView;

import com.tbruyelle.rxpermissions2.RxPermissions;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.fragment.BaseFragment;
import cn.dankal.basiclib.model.BaseModel;
import cn.dankal.basiclib.model.SampleModel;
import cn.dankal.basiclib.model.SubmitRecordModel;
import cn.dankal.basiclib.rx.MapFunc;
import cn.dankal.basiclib.rx.NormalSubscriber;
import cn.dankal.yunlaboratory.R;
import cn.dankal.yunlaboratory.ui.activity.GetSampleActivity;
import cn.dankal.yunlaboratory.ui.activity.ScanActivity;
import cn.dankal.yunlaboratory.ui.dialog.GetBackSampleDialog;
import cn.dankal.yunlaboratory.ui.dialog.InputPersonNoDialog;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class ScanFragment extends BaseFragment {

    public static final int RC_SCAN = 0x3;

    @BindView(R.id.tv_hello_name)
    TextView tvHelloName;
    @BindView(R.id.card_scan)
    CardView cardScan;
    private GetBackSampleDialog getBackSampleDialog;
    private String codeResult;
    private InputPersonNoDialog inputPersonNoDialog;

    public static ScanFragment getInstance() {
        ScanFragment scanFragment = new ScanFragment();
        return scanFragment;
    }

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

    @Override
    protected void initComponents() {
        getBackSampleDialog = new GetBackSampleDialog(getContext());
        inputPersonNoDialog = new InputPersonNoDialog(getContext());

        getBackSampleDialog.setViewClick(R.id.tv_positive, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBackSampleDialog.dismiss();
                inputPersonNoDialog.show();
            }
        });

        if (DKUserManager.getUserInfo()!=null&&!TextUtils.isEmpty(DKUserManager.getUserInfo().getName())){
            tvHelloName.setText("Hello，"+DKUserManager.getUserInfo().getName());
        }


        inputPersonNoDialog.setViewClick(R.id.tv_positive, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nameStr = inputPersonNoDialog.getInputName();
                if (!TextUtils.isEmpty(nameStr)) {
                    inputPersonNoDialog.dismiss();
                    UserServiceFactory.sampleNext(nameStr,codeResult).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel<SubmitRecordModel>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            showLoadingDialog();
                        }

                        @Override
                        public void onNext(BaseModel<SubmitRecordModel> submitRecordModelBaseModel) {
                            SubmitRecordModel model = submitRecordModelBaseModel.getData();
                            Intent intent = new Intent(getContext(), GetSampleActivity.class);
                            model.setSample_num(codeResult);
                            intent.putExtra("data", model);
                            getContext().startActivity(intent);
                        }

                        @Override
                        public void onComplete() {
                            super.onComplete();
                            dismissLoadingDialog();
                        }
                    });
                }


            }
        });
    }


    @OnClick(R.id.card_scan)
    public void onViewClicked() {
        new RxPermissions(getActivity()).request(Manifest.permission.CAMERA).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    Intent intent = new Intent(getContext(), ScanActivity.class);
                    startActivityForResult(intent, RC_SCAN);
                }
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == RC_SCAN) {
            codeResult = data.getStringExtra("data");
            requestData();

        }
    }

    /**
     * 判断是否是第一次取样
     */
    public void requestData(){
        UserServiceFactory.sample(codeResult).map(new MapFunc<BaseModel<SampleModel>>()).subscribe(new NormalSubscriber<BaseModel<SampleModel>>() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoadingDialog();
            }

            @Override
            public void onNext(BaseModel<SampleModel> sampleModelBaseModel) {
                SampleModel sampleModel = sampleModelBaseModel.getData();
                if (sampleModel.getType() == 1) {
                    getBackSampleDialog.show();
                } else {
                    Intent intent = new Intent(getContext(), GetSampleActivity.class);
                    SubmitRecordModel model = sampleModelBaseModel.getData().getUpload_list();
                    model.setSample_num(codeResult);
                    intent.putExtra("data", model);
                    getContext().startActivity(intent);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissLoadingDialog();
            }
        });
    }
}
