package com.yutou.tools.Tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yutou.tools.mybatis.dao.PermissionDao;
import com.yutou.tools.mybatis.dao.UKeyDao;
import com.yutou.tools.mybatis.model.Permission;
import com.yutou.tools.mybatis.model.PermissionExample;
import com.yutou.tools.mybatis.model.UKey;
import com.yutou.tools.mybatis.model.UKeyExample;
import com.yutou.tools.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class AuthManagerController {
    @Resource
    UKeyDao keyDao;
    @Resource
    PermissionDao permissionDao;

    @ResponseBody
    @RequestMapping("/tools/auth/all.do")
    public JSONObject getUserList() {
        JSONObject json = new JSONObject();
        JSONArray array = new JSONArray();
        List<UKey> list = keyDao.selectByExample(new UKeyExample());
        for (UKey uKey : list) {
            JSONObject item = new JSONObject();
            item.put("id", uKey.getId());
            item.put("key", uKey.getKey());
            item.put("authSize", JSONArray.parseArray(uKey.getPower()).size());
            array.add(item);
        }
        json.put("code", 0);
        json.put("msg", "ok");
        json.put("data", array);
        json.put("count", array.size());
        return json;
    }

    @ResponseBody
    @RequestMapping("/tools/auth/user.do")
    public JSONObject getUserAuth(String uid) {
        JSONObject json = new JSONObject();
        if (ObjectUtils.isEmpty(uid)) {
            json.put("code", -1);
            json.put("msg", "uid为空");
            return json;
        }
        JSONArray allPermission = new JSONArray();
        JSONArray userPermission = new JSONArray();
        UKey key = keyDao.selectByPrimaryKey(Integer.parseInt(uid));
        List<String> permission = Tools.getUrls("com.yutou.tools", null);
        JSONArray powers = JSONArray.parseArray(key.getPower());
        System.out.println("总权限:" + permission.size());
        JSONObject admin = new JSONObject();
        admin.put("value", "-1");
        admin.put("title", "管理员");
        admin.put("disabled", "");
        admin.put("checked", "");
        allPermission.add(admin);
        List<String> list = powers.toJavaList(String.class);
        if (list.contains("-1")) {
            userPermission.add("-1");
        }

        int index = 1;
        for (String per : permission) {
            index++;
            JSONObject item = new JSONObject();
            item.put("value", index + "");
            item.put("title", per);
            item.put("disabled", "");
            item.put("checked", "");
            PermissionExample example = new PermissionExample();
            example.createCriteria().andUrlEqualTo(per);
            List<Permission> permissions = permissionDao.selectByExample(example);
            allPermission.add(item);
            if (permissions.size() == 0) {
                Permission p = new Permission();
                p.setTitle(per);
                p.setUrl(per);
               permissionDao.insert(p);
            } else {
                permissions.get(0).getId();
                if (list.contains(permissions.get(0).getId() + "")) {
                    userPermission.add(index + "");
                }
            }
        }
        JSONObject data = new JSONObject();
        data.put("all", allPermission);
        data.put("user", userPermission);
        json.put("data", data);
        json.put("msg", "ok");
        json.put("code", 0);
        return json;
    }
    @ResponseBody
    @RequestMapping("/a/t.do")
    public JSONObject test(){
        return new JSONObject();
    }
    @ResponseBody
    @RequestMapping("/tools/auth/set.do")
    public JSONObject updateAuth(String uid, String auth, String type) {
        JSONObject json = new JSONObject();
        System.out.println("uid = " + uid + ", auth = " + auth + ", type = " + type);
        JSONArray array = JSONArray.parseArray(auth);
        UKey key = keyDao.selectByPrimaryKey(Integer.parseInt(uid));
        JSONArray powers = JSONArray.parseArray(key.getPower());
        if ("add".equals(type) && auth.contains("管理员")) {
            powers.clear();
            powers.add("-1");
        } else {
            for (Object o : array) {
                JSONObject item = (JSONObject) o;
                PermissionExample example = new PermissionExample();
                example.createCriteria().andUrlEqualTo(item.getString("auth"));
                List<Permission> list = permissionDao.selectByExample(example);
                Permission permission = null;
                if (list.size() > 0) {
                    permission = list.get(0);
                }
                if ("add".equals(type)) {
                    if (permission != null) {
                        powers.remove("-1");
                        powers.add(permission.getId() + "");
                    }
                } else if ("remove".equals(type)) {
                    if (permission == null) {
                        powers.remove("-1");
                    } else {
                        System.out.println("移除:" + permission.getId());
                        powers.remove(permission.getId() + "");
                    }
                }
                System.out.println("设置权限:" + powers.toJSONString());
            }
        }
        key.setPower(powers.toJSONString());
        keyDao.updateByPrimaryKey(key);
        json.put("code", 0);
        json.put("msg", "操作成功");
        return json;
    }
}
