package com.bonait.bnframework.modules.mine.fragment;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPE;
import com.bonait.bnframework.common.db.mode.BPA_SILOS;
import com.bonait.bnframework.common.db.mode.BPA_SILOSANDMATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_USER;
import com.bonait.bnframework.common.helper.I.IRunT;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.http.callback.json.JsonDialogCallback;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.model.ResAPI;
import com.bonait.bnframework.common.model.mode.ActionJsonMode;
import com.bonait.bnframework.common.model.mode.BatchingInfo;
import com.bonait.bnframework.common.model.mode.BomTechnologyActionInfo;
import com.bonait.bnframework.common.model.mode.CloudGood;
import com.bonait.bnframework.common.model.mode.GoodsTechnology;
import com.bonait.bnframework.common.model.mode.TechnologyAction;
import com.bonait.bnframework.common.model.mode.UpdateBomInfo;
import com.bonait.bnframework.common.model.mode.UpdateCloudGood;
import com.bonait.bnframework.common.oss.OssHelper;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.common.view.AddUserDialog;
import com.bonait.bnframework.common.view.GoodEditDialog;
import com.bonait.bnframework.modules.home.adapter.user_adapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class UserActivity extends BaseActivity implements MyClickListener {
    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    @BindView(R.id.add_wl)
    Button add_wl;
    @BindView(R.id.datatab)
    ListView datatab;
    /**
     * 用户数据
     */
    ArrayList<BPA_USER> bpa_users = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        //属性绑定
        ButterKnife.bind(this);
        initTopBar();
        InitView();
    }

    private void initTopBar() {
        mTopBar.setTitle("账户管理");
        mTopBar.addLeftImageButton(R.mipmap.fanhui, R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 初始化界面
     */
    public void InitView() {
        Initdata();
    }

    @OnClick({R.id.add_wl, R.id.synchronous})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_wl://新增用户

                final AddUserDialog editDialog = new AddUserDialog(this);
                editDialog.setTitle("角色列表");
                editDialog.setYesOnclickListener("确定", new AddUserDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick(String name, String acc, String pass, String qx) {

                        if (name.toString().equals("admin") || acc.toString().equals("admin")) {
                            ToastUtils.warning("不能设置账号为admin！");
                            return;
                        }

                        BPA_USER bpa_user = new BPA_USER();
                        bpa_user.name = name;
                        bpa_user.account = acc;
                        bpa_user.pass = pass;
                        bpa_user.deviceID = ConfigName.getInstance().DeviceId;
                        bpa_user.userID =qx;//更改为,用户权限 // 一般用户 研发用户 超级管理员
                        if(QueryDB.GetUserIsCZ(bpa_user.account,bpa_user.name))
                        {
                            ToastUtils.warning("已有该账户,请重新填写账户信息！");
                            return;
                        }
                        QueryDB.AddUser(bpa_user);
                        ToastUtils.info("新增成功");
                        Initdata();
                        editDialog.dismiss();
                    }
                });
                editDialog.setNoOnclickListener("取消", new AddUserDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        editDialog.dismiss();
                    }
                });
                editDialog.show();
                break;

            case R.id.synchronous://新增用户

                String title = "温馨提示！";
                String message = "客官确定要恢复出厂账户吗？";
                AlertDialogUtils.showDialog(this, title, message, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        ArrayList<BPA_USER> users=QueryDB.GetUserALL();
                        for (BPA_USER item:users)
                        {
                            QueryDB.DeleteUser(item);
                        }
                        BPA_USER user = new BPA_USER();
                        user.id = "20230415-fyf";
                        user.name = "管理员";
                        user.account = "admin";
                        user.pass = "123456";
                        user.deviceID = ConfigName.getInstance().DeviceId;
                        user.userID = "超级管理员";
                        user.exp = "管理员";
                        QueryDB.AddUser(user);

                        BPA_USER user1 = new BPA_USER();
                        user1.name = "一般用户";
                        user1.account = "ybyh";
                        user1.pass = "123456";
                        user1.deviceID = ConfigName.getInstance().DeviceId;
                        user1.userID = "一般用户";
                        user1.exp = "一般用户";
                        QueryDB.AddUser(user1);


                        BPA_USER user2 = new BPA_USER();
                        user2.name = "研发用户";
                        user2.account = "yfyh";
                        user2.pass = "123456";
                        user2.deviceID = ConfigName.getInstance().DeviceId;
                        user2.userID = "研发用户";
                        user2.exp = "研发用户";
                        QueryDB.AddUser(user2);

                        ToastUtils.info("恢复出厂成功！");
                        Initdata();
                        dialog.dismiss();
                    }
                });
                break;
        }
    }


    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {
            bpa_users = QueryDB.GetUserALL();
            user_adapter adapter = new user_adapter(getBaseContext(), R.layout.user_item, bpa_users, this);
            datatab.setAdapter(adapter);
        } catch (Exception e) {

        }
    }

    @Override
    public void clickListener(View v, Object data) {
        String name = ((BPA_USER) data).name;
        String ac = ((BPA_USER) data).account;
        if (name.equals("admin") || ac.equals("admin")) {
            ToastUtils.warning("对不起,admin账户禁止操作！");
            return;
        }
        switch (v.getId()) {
            case R.id.name://修改物料名称按钮
                final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
                builder.setTitle("用户名称")
                        .setPlaceholder("在此输入用户名称")
                        .setDefaultText(((BPA_USER) data).name)
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    if (text.toString().equals("admin")) {
                                        ToastUtils.warning("不能设置账号为admin！");
                                        return;
                                    }

                                    if(QueryDB.GetUserIsCZ(((BPA_USER) data).account,text.toString()))
                                    {
                                        ToastUtils.warning("已有该账户,请重新填写账户信息！");
                                        return;
                                    }
                                    QueryDB.UpdateUserName(((BPA_USER) data).id, text.toString());
                                    Initdata();
                                    ToastUtils.info("修改成功");
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.warning("不能为空！");
                                }
                            }
                        })
                        .show();
                break;
            case R.id.zhanghao://修改物料名称按钮
                final QMUIDialog.EditTextDialogBuilder builder1 = new QMUIDialog.EditTextDialogBuilder(this);
                builder1.setTitle("用户账号")
                        .setPlaceholder("在此输入用户账号")
                        .setDefaultText(((BPA_USER) data).account)
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder1.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    if (text.toString().equals("admin")) {
                                        ToastUtils.warning("不能设置账号为admin！");
                                        return;
                                    }
                                    if(QueryDB.GetUserIsCZ(text.toString(),((BPA_USER) data).name))
                                    {
                                        ToastUtils.warning("已有该账户,请重新填写账户信息！");
                                        return;
                                    }

                                    QueryDB.UpdateAccount(((BPA_USER) data).id, text.toString());
                                    Initdata();
                                    ToastUtils.info("修改成功");
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.info("不能为空！");
                                }
                            }
                        })
                        .show();
                break;
            case R.id.mima://修改物料名称按钮
                final QMUIDialog.EditTextDialogBuilder builder2 = new QMUIDialog.EditTextDialogBuilder(this);
                builder2.setTitle("用户密码")
                        .setPlaceholder("在此输入用户密码")
                        .setDefaultText(((BPA_USER) data).pass)
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder2.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    QueryDB.UpdateUserPass(((BPA_USER) data).id, text.toString());
                                    Initdata();
                                    ToastUtils.info("修改成功");
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.info("不能为空！");
                                }
                            }
                        })
                        .show();
                break;

            case R.id.quanxian://修改物料名称按钮
                final String [] items=ConfigName.getInstance().UserRole.toArray(new String[0]);
                int checkIndex=1;
                String qx=((BPA_USER) data).userID;
                for (int k=0;k<ConfigName.getInstance().UserRole.size();k++)
                {
                    if(qx!=null && !qx.isEmpty() && qx.contains(items[k]))
                    {
                        checkIndex=k;
                    }
                }
                new QMUIDialog.CheckableDialogBuilder(this)
                        .addItems(items, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String names=items[which];
                                if (names != null && !names.isEmpty()) {
                                    QueryDB.UpdateUserRole(((BPA_USER) data).id, names);
                                    Initdata();
                                    ToastUtils.info("修改成功");
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.info("不能为空！");
                                    return;
                                }
                                dialog.dismiss();
                            }
                        })
                        .setCheckedIndex(checkIndex)
                        .show();
                break;

            case R.id.button_item://删除按钮
                QueryDB.DeleteUser((BPA_USER) data);
                Initdata();
                ToastUtils.info("删除成功");
                break;
        }
    }

    @Override
    public void clickListenerNew(View v, int k, Object data) {

    }
}