package com.tjzhxx.union.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

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.UserInfo;
import com.tjzhxx.union.entity.request.SubnewworkerRequest;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.FileUtils;
import com.tjzhxx.union.public_store.LogUtils;
import com.tjzhxx.union.public_store.SpUtils;
import com.tjzhxx.union.public_store.TimeUtil;
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 SignatureSureActivity extends BaseActivity {

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.idcard)
    TextView idcard;
    @BindView(R.id.linepath)
    LinePathView linepath;
    private SubnewworkerRequest request;
    private UnionServices services;
    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_signature_sure;
    }

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        initTitle("签名确认");
        request = (SubnewworkerRequest)getIntent().getSerializableExtra("SubnewworkerRequest");
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
        name.setText(request.getWname());
        time.setText(TimeUtil.getCurrentDayStr(TimeUtil.TIME_YYYY_MM_DD));
        phone.setText(request.getTelno());
        idcard.setText(request.getSfzid());

    }

    @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(SignatureSureActivity.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) {
                        request.setSignurl(value.getData());
                        subnewworkerfromapp();
                    }

                });
    }

    //提交会员入会资料
    private void subnewworkerfromapp() {
        LogUtils.e("TAG",request.toString());
        request.setCity(ConstDefine.city);
        request.setDisc(ConstDefine.district);
        services.subnewworkerfromapp(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<UserInfo>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<UserInfo>>(this) {
                    @Override
                    public void success(BaseResponse<UserInfo> value) {
                        ConstDefine.userInfo = value.getData();
                        SpUtils.putUserInfo(SignatureSureActivity.this,value.getData());
                        Intent intent = new Intent(SignatureSureActivity.this,PayActivity.class);
                        startActivity(intent);
                        finish();
                    }

                });
    }
}
