package com.tjzhxx.union.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tjzhxx.union.R;
import com.tjzhxx.union.adapter.RescuestyleAdapter;
import com.tjzhxx.union.entity.BaseResponse;
import com.tjzhxx.union.entity.Doclist;
import com.tjzhxx.union.entity.Rescuestyle;
import com.tjzhxx.union.entity.Resucekey;
import com.tjzhxx.union.entity.request.DataDbqzPhotoRequest;
import com.tjzhxx.union.entity.request.DataDbqzRequest;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.LogUtils;
import com.tjzhxx.union.public_store.retrofit.RetrofitUtils;
import com.tjzhxx.union.public_store.retrofit.UnionServices;
import com.tjzhxx.union.public_store.rx.DefaultSubscriber;
import com.tjzhxx.union.public_store.rx.ProgressSubscriber;
import com.tjzhxx.union.system.BaseActivity;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 描述:
 *
 * @author yhx
 * @create 2021/9/7  11:45 PM
 */
public class DataEditeDbjzActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<Rescuestyle> list = new ArrayList<>();
    private List<Resucekey> resucekeys = new ArrayList<>();
    private List<Doclist> doclist = new ArrayList<>();
    private String applyid;
    private RescuestyleAdapter adapter;
    private UnionServices services;

    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_data_edite_dbqz;
    }

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        initTitle("大病救助资料填写");
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RescuestyleAdapter(this, list);
        recyclerView.setAdapter(adapter);
        String json = getIntent().getStringExtra("json");
        Gson gson = new Gson();
        Type type = new TypeToken<List<Resucekey>>() {
        }.getType();
        resucekeys =  gson.fromJson(json, type);
        DataDbqzRequest request = new DataDbqzRequest();
        request.setApplytelno(ConstDefine.userInfo.getTelno());
        request.setWid(ConstDefine.userInfo.getWid());
        request.setRescuestyle("3");
        request.setValuelist(new Gson().toJson(resucekeys));
        LogUtils.e("TAG",request.toString());
        subapplywithinfoforapp(request);
        queryrescuelistforapp();
    }


    @OnClick({R.id.submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit:
                submit();
                break;
        }
    }

    public void submit() {
        doclist.clear();
        for (Rescuestyle rescuestyle:list){
            if (rescuestyle.getIsmust().equals("1") && TextUtils.isEmpty(rescuestyle.getImageUrl())){
                showSnackBar("请上传"+rescuestyle.getDockind());
                return;
            }else {
                Doclist bean = new Doclist();
                bean.setDocstyle(rescuestyle.getDockind());
                bean.setDocurl(rescuestyle.getImageUrl());
                doclist.add(bean);
            }
        }
        DataDbqzPhotoRequest request = new DataDbqzPhotoRequest();
        request.setApplyid(applyid);
        request.setDoclist(new Gson().toJson(doclist));
        subdocbyapplyidforapp(request);
    }

    private void queryrescuelistforapp() {
        services.queryrescuelistforapp(3)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<List<Rescuestyle>>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<List<Rescuestyle>>>(this) {
                    @Override
                    public void success(BaseResponse<List<Rescuestyle>> value) {
                        if (value.getData() != null) {
                            list.clear();
                            list.addAll(value.getData());
                            adapter.notifyDataSetChanged();
                        }
                    }

                });
    }

    private void subapplywithinfoforapp(DataDbqzRequest request) {
        services.subapplywithinfoforapp(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<String>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<String>>(this) {
                    @Override
                    public void success(BaseResponse<String> value) {
                        applyid = value.getData();
                    }

                });
    }

    private void subdocbyapplyidforapp(DataDbqzPhotoRequest request) {
        services.subdocbyapplyidforapp(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new ProgressSubscriber<BaseResponse>(this) {
                    @Override
                    public void success(BaseResponse value) {
                        showSnackBar("提交成功");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finish();
                            }
                        }, 1500);
                    }

                });
    }
}
