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

import android.content.Intent;
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.WorkToolBarActivity;
import com.wd.workoffice.bean.UserInfoBean;
import com.wd.workoffice.ui.activity.login.MainLoginActivity;
import com.wd.workoffice.util.DataKeeper;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.model.event.LoginEvent;

/**
 * 我的 - 设置
 * 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);
        if (!TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo())) {
            String userInfo = UserKeeper.getInstance().getUserInfo();
            if (!TextUtils.isEmpty(userInfo)) {
                UserInfoBean userInfoBean = JSON.parseObject(userInfo, UserInfoBean.class);
                tvName.setText(userInfoBean.getUserName());
                tvPhone.setText(userInfoBean.getPhone());
            }
        } else {
            rlPwd.setVisibility(View.GONE);
            tvName.setText(UserKeeper.getInstance().getMesNickName());
            tvPhone.setText(UserKeeper.getInstance().getMesMobile());
        }
    }

    @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() {
        toast("退出成功");
        if (!TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()))
            WorkUtils.handleAlias(Integer.valueOf(UserKeeper.getInstance().getUserId()),1);
        Intent intent = new Intent(UserSettingActivity.this, MainLoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        UserKeeper.getInstance().clearAll();
        DataKeeper.getInstance().clearAll();
        EventBus.getDefault().post(new LoginEvent());
        startActivity(intent);
//        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, MainLoginActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                UserKeeper.getInstance().clearAll();
//                startActivity(intent);
//            }
//        });
    }
}
