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

import android.os.Bundle;
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.ContactsBean;
import com.wd.workoffice.bean.PermissionBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
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 UserPermissionActivity extends WorkToolBarActivity {
    @BindView(R.id.tv_role)
    TextView tvRole;
    @BindView(R.id.tv_data)
    TextView tvData;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    private PermissionBean dataBean;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        RtfUtils.getRtf().permissionDetail().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;
                }
                dataBean = JSON.parseObject(data.getData().toString(), PermissionBean.class);
                tvRole.setText(getRoleString(dataBean.getRoles()));
                tvData.setText(dataBean.getUserAllDataScope().getMax_data_scope_name());
                tvDep.setText(getDepString(dataBean.getDeptPermissions()));
            }
        });
    }

    protected String getDepString(List<PermissionBean.DeptPermissionsBean> data) {
        String returnData = "";
        if (data != null && data.size() != 0) {
            for (PermissionBean.DeptPermissionsBean process : data) {
                returnData = returnData + process.getName() + ",";
            }
            returnData = returnData.substring(0, returnData.length() - 1);
        } else {
            returnData = "无";
        }
        return returnData;
    }
    protected String getRoleString(List<PermissionBean.RolesBean> data) {
        String returnData = "";
        if (data != null && data.size() != 0) {
            for (PermissionBean.RolesBean process : data) {
                returnData = returnData + process.getName() + ",";
            }
            returnData = returnData.substring(0, returnData.length() - 1);
        } else {
            returnData = "无";
        }
        return returnData;
    }

    @Override
    protected void initEvent() {
    }

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