package com.tjzhxx.union.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.lifecycle.Lifecycle;

import com.tjzhxx.union.R;
import com.tjzhxx.union.entity.BaseResponse;
import com.tjzhxx.union.entity.UserInfo;
import com.tjzhxx.union.entity.request.SmsCodeRequest;
import com.tjzhxx.union.entity.request.SmsLoginRequest;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.SpUtils;
import com.tjzhxx.union.public_store.retrofit.RetrofitUtils;
import com.tjzhxx.union.public_store.retrofit.UnionServices;
import com.tjzhxx.union.public_store.rx.ProgressSubscriber;
import com.tjzhxx.union.system.BaseActivity;
import com.tjzhxx.union.system.WebviewActivity;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

import java.util.Timer;
import java.util.TimerTask;

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 LoginActivity extends BaseActivity {
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.check)
    ImageView check;
    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.new_login)
    TextView newLogin;
    @BindView(R.id.wx)
    ImageView wx;
    private UnionServices services;
    private static int m = 60;
    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
    }


    @OnClick({R.id.icon_back, R.id.get_code, R.id.login, R.id.new_login, R.id.wx})
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.icon_back:
                finish();
                break;
            case R.id.get_code:
                if (getCode.getText().equals("获取验证码")) {
                    if (TextUtils.isEmpty(phone.getText().toString())) {
                        showSnackBar("请输入手机号");
                        return;
                    }
                    sendSmsCode();
                }
                break;
            case R.id.login:
                if (TextUtils.isEmpty(code.getText().toString())){
                    showSnackBar("请输入验证码");
                    return;
                }
                smsLogin();
                break;
            case R.id.new_login:
                intent.setClass(this, WebviewActivity.class);
                intent.putExtra("url", "http://47.94.42.74:9100/zhghsjd/index.html#/guidepage");
                intent.putExtra("title", "会员入会");
                startActivity(intent);
                break;
            case R.id.wx:
                break;
        }
    }
    private void sendSmsCode() {
        SmsCodeRequest request = new SmsCodeRequest();
        request.setTel(phone.getText().toString());
        services.sendSmsCode(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) {
                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        m--;
                                        if (m <= 0) {
                                            getCode.setText("获取验证码");
                                            cancel();
                                            m = 60;
                                            return;
                                        }
                                        getCode.setText(m + "秒");
                                    }
                                });
                            }
                        }, 10, 1000);
                    }

                });
    }

    private void smsLogin() {
        SmsLoginRequest request = new SmsLoginRequest();
        request.setTel(phone.getText().toString());
        request.setSmscode(code.getText().toString());
        services.smsLogin(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<UserInfo>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new ProgressSubscriber<BaseResponse<UserInfo>>(this) {
                    @Override
                    public void success(BaseResponse<UserInfo> value) {
                        ConstDefine.userInfo = value.getData();
                        SpUtils.putUserInfo(LoginActivity.this,value.getData());
                        finish();
                    }

                });
    }
}
