package com.wd.workoffice.ui.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.PermissionBean;
import com.wd.workoffice.bean.UserInfoBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.MainActivity;
import com.wd.workoffice.ui.activity.login.LoginActivity;
import com.wd.workoffice.util.UserKeeper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 我的 - 设置
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class UserSettingActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.rl_account)
    RelativeLayout rlAccount;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.rl_phone)
    RelativeLayout rlPhone;
    @BindView(R.id.rl_pwd)
    RelativeLayout rlPwd;
    @BindView(R.id.rl_about)
    RelativeLayout rlAbout;
    @BindView(R.id.btn_logout)
    Button btnLogout;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        String userInfo = UserKeeper.getInstance().getUserInfo();
        if (!TextUtils.isEmpty(userInfo)) {
            UserInfoBean userInfoBean = JSON.parseObject(userInfo, UserInfoBean.class);
            tvName.setText(userInfoBean.getUserName());
            tvPhone.setText(userInfoBean.getPhone());
        }
    }

    @Override
    protected void initData() {
    }


    @Override
    protected void initEvent() {
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_user_setting;
    }


    @OnClick({R.id.rl_account, R.id.rl_phone, R.id.rl_pwd, R.id.rl_about, R.id.btn_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_pwd:
                startActivity(UserModifyPwdActivity.class);
                break;
            case R.id.rl_about:
                startActivity(UserAboutActivity.class);
                break;
            case R.id.btn_logout:
                logout();
                break;
        }
    }

    private void logout() {
        RtfUtils.getRtf().logout().compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {


            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                toast("退出成功");
                Intent intent = new Intent(UserSettingActivity.this, LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                UserKeeper.getInstance().clearAll();
                startActivity(intent);
            }
        });
    }
}
