package com.tjzhxx.union.activity;

import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.Lifecycle;

import com.tjzhxx.union.R;
import com.tjzhxx.union.custom_view.LinePathView;
import com.tjzhxx.union.entity.BaseResponse;
import com.tjzhxx.union.entity.request.GetWelfareRequest;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.FileUtils;
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.system.BaseActivity;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 描述:
 *
 * @author yhx
 * @create 2021/9/2  8:57 AM
 */
public class WelfareSignatureSureActivity extends BaseActivity {


    @BindView(R.id.linepath)
    LinePathView linepath;
    private UnionServices services;
    private int wfid;
    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_welfare_signature_sure;
    }

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        initTitle("签名领取");
        wfid = getIntent().getIntExtra("wfid",0);
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
    }

    @OnClick({R.id.clear, R.id.sure})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.clear:
                linepath.clear();
                break;
            case R.id.sure:
                if (!linepath.getTouched()){
                    showSnackBar("请输入签名");
                    return;
                }
                try {
                    String tiemStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
                    String path = FileUtils.getCompressFilePath(WelfareSignatureSureActivity.this)  + "/" + tiemStamp + ".png";
                    linepath.save(path);
                    uploadImage(path);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    //上传图片
    private void uploadImage(String path) {
        File file = new File(path);
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part imageBodyPart = MultipartBody.Part.createFormData("file", file.getName(), imageBody);
        services.uploadImage(imageBodyPart)
                .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) {
                        getwelfarebywid(value.getData());
                    }

                });
    }

    private void getwelfarebywid(String url) {
        GetWelfareRequest request = new GetWelfareRequest();
        request.setWfid(wfid);
        request.setWid(Integer.valueOf(ConstDefine.userInfo.getWid()));
        request.setSignpicurl(url);
        services.getwelfarebywid(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse>(this) {
                    @Override
                    public void success(BaseResponse value) {
                       finish();
                    }
                });
    }
}
