package com.dskj.trustlend.fragment;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.azhon.basic.base.BaseLazyFragment;
import com.bumptech.glide.Glide;
import com.dskj.trustlend.AboutUsActivity;
import com.dskj.trustlend.EditUserActivity;
import com.dskj.trustlend.InitApp;
import com.dskj.trustlend.LoginActivity;
import com.dskj.trustlend.R;
import com.dskj.trustlend.api.Api;
import com.dskj.trustlend.api.BaseObserver;
import com.dskj.trustlend.api.Result;
import com.dskj.trustlend.bean.UserBean;
import com.dskj.trustlend.databinding.FragmentMeBinding;
import com.dskj.trustlend.dialog.LogoutDialog;
import com.dskj.trustlend.home.MainViewModel;
import com.dskj.trustlend.utils.GsonUtils;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * 主页模块
 * @author xuhuixiang
 */
public class MeFragment extends BaseLazyFragment<MainViewModel, FragmentMeBinding>  {
    public static final String TAG = "HomeFragment";
    UserBean userBean;
    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_me;
    }

    @Override
    protected void setUpView() {
        dataBinding.logoutBt.setOnClickListener(v -> {
            LogoutDialog logoutDialog = new LogoutDialog(getActivity());
            logoutDialog.setLeftButton(v1 -> logoutDialog.dismiss());
            logoutDialog.setRightButton(v12 -> {

                Api.getInstance().logout()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<Result>() {

                            @Override
                            public void onSuccess(Result feedbackResp) {
                                InitApp.setUserToken("");
                                logoutDialog.dismiss();
                                startActivity(new Intent(getActivity(),LoginActivity.class));
                                getActivity().finish();
                            }

                            @Override
                            public void onError(int code, String msg) {
                                InitApp.setUserToken("");
                                logoutDialog.dismiss();
                                startActivity(new Intent(getActivity(),LoginActivity.class));
                                getActivity().finish();
                            }
                        });


            });
            logoutDialog.show();
        });
        dataBinding.editIv.setOnClickListener(v -> startActivityForResult(new Intent(getActivity(), EditUserActivity.class),999));
        dataBinding.aboutTv.setOnClickListener(v -> startActivity(new Intent(getActivity(), AboutUsActivity.class)));

        userBean = InitApp.getUserBean();
        changeUserInfo(userBean);

    }

    private void changeUserInfo(UserBean userBean) {
        dataBinding.fullNameTv.setText(userBean.getFull_name()+"");
        dataBinding.idcardTv.setText("ID number："+userBean.getId_number());
        dataBinding.mobileTv.setText("mobile："+userBean.getPhone());
        Glide.with(getActivity()).load(userBean.getAvatar()+"").error(R.mipmap.default_head_img).error(R.mipmap.default_head_img).into(dataBinding.userHeadIv);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 999){
//            ToastUtils.showShort(dataBinding.fullNameTv,"有回调了");
            getUserInfo();
        }
    }

    @Override
    public void onResume() {
        super.onResume();

    }
    private void getUserInfo() {
        Api.getInstance().userProfile()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Result<UserBean>>() {

                    @Override
                    public void onSuccess(Result<UserBean> feedbackResp) {
                        if(feedbackResp!=null&&feedbackResp.data!=null) {
                            userBean.setAvatar(feedbackResp.data.getAvatar());
                            userBean.setEmail(feedbackResp.data.getEmail());
                            InitApp.saveString("user_bean", GsonUtils.beanToJSONString(userBean));
//                            InitApp.setUserToken(feedbackResp.data.get(0).getAuth());
                            changeUserInfo(feedbackResp.data);
                        }
                    }

                    @Override
                    public void onError(int code, String msg) {

                    }
                });
    }


    @Override
    protected void setUpData() {
        dataBinding.setModel(viewModel);
        dataBinding.setLifecycleOwner(this);
    }


    @Override
    protected MainViewModel initViewModel() {
        return ViewModelProviders.of(getActivity()).get(MainViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }

    @Override
    protected void lazyLoad() {
        getUserInfo();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }

}
