package com.lightinit.hsdatashow.common;

import com.lightinit.hsdatashow.entity.Menu;
import com.lightinit.hsdatashow.entity.MenuAttributesEntity;
import com.lightinit.hsdatashow.entity.MenuDataEntity;
import com.lightinit.hsdatashow.entity.RoleModule;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/13.
 */
public class MenuUtils {
    private static  String[] ignoreProperties = new String[1];
    static {
        ignoreProperties[0]="children";
    }
    public static List<MenuDataEntity> InitSystemBottomMenu(){
        List<MenuDataEntity> list=new ArrayList<MenuDataEntity>();
        String sysMenuJson="[{\"id\":400,\"text\":\"用户信息\",\"iconCls\":\"fi-torso\",\"attributes\":{\"code\":\"\",\"action\":\"/user/view/password.action\"}},{\"id\":500,\"text\":\"注销用户\",\"iconCls\":\"fi-power\",\"attributes\":{\"code\":\"\",\"action\":\"dialog\",\"type\":\"dialog\"}}]";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, MenuDataEntity.class);
            list= objectMapper.readValue(sysMenuJson, javaType);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    public static List<MenuDataEntity> InitSystemTopMenu(){
        List<MenuDataEntity> list=new ArrayList<MenuDataEntity>();
        String sysMenuJson="[{\"id\":100,\"text\":\"配置中心\",\"iconCls\":\"fi-thumbnails\",\"state\":\"closed\",\"children\":[{\"id\":101,\"text\":\"模块配置\",\"iconCls\":\"fi-social-evernote\",\"attributes\":{\"action\":\"/menu/view/list.action\",\"type\":\"tab\",\"code\":\"\"}},{\"id\":102,\"text\":\"日志管理\",\"iconCls\":\"fi-page-multiple\"},{\"id\":103,\"text\":\"Druid管理\",\"iconCls\":\"fi-social-evernote\",\"attributes\":{\"code\":\"\",\"action\":\"/druid/index.html\",\"type\":\"tab\",\"iframe\":\"true\"}}]},{\"id\":200,\"text\":\"用户中心\",\"state\":\"closed\",\"iconCls\":\"fi-torsos-all\",\"children\":[{\"id\":201,\"text\":\"角色管理\",\"iconCls\":\"fi-lock\",\"attributes\":{\"code\":\"\",\"action\":\"/role/view/list.action\"}},{\"id\":202,\"text\":\"用户管理\",\"iconCls\":\"fi-torsos\",\"attributes\":{\"code\":\"\",\"action\":\"/user/view/list.action\",\"type\":\"tab\"}},{\"id\":203,\"text\":\"权限管理\",\"iconCls\":\"fi-layout\",\"attributes\":{\"code\":\"\",\"action\":\"/grant/view/list.action\",\"type\":\"tab\"}}]}]";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, MenuDataEntity.class);
            list= objectMapper.readValue(sysMenuJson, javaType);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    public static MenuDataEntity MenuConverter(Menu menu, List<RoleModule> roleModuleList, boolean isSuperUser){
        MenuDataEntity menuDataEntity=new MenuDataEntity();
        if (menu!=null && (isSuperUser || HasModulePermission(menu.getId(),roleModuleList))){
            menuDataEntity.setId(Integer.valueOf(menu.getId().toString()));
            menuDataEntity.setText(menu.getMenuname());
            String iconCls= menu.getMenuicon();
            if(StringUtils.isEmpty(iconCls))
                iconCls="fi-graph-trend";
            menuDataEntity.setIconCls(iconCls);
            MenuAttributesEntity attributes=new MenuAttributesEntity();
            attributes.setAction(menu.getMenuaction());
            attributes.setType(menu.getMenushowtype());
            attributes.setCode(menu.getMenucode());
            menuDataEntity.setAttributes(attributes);
            if (menu.getChildren()!=null && menu.getChildren().size()>0){
                menuDataEntity.setState("closed");
                List<MenuDataEntity> children=new ArrayList<MenuDataEntity>();
                for (Menu item:menu.getChildren()){
                    if (item==null)
                        continue;;
                    MenuDataEntity _item=MenuConverter(item,roleModuleList,isSuperUser);
                    if(_item!=null)
                        children.add(_item);
                }
                menuDataEntity.setChildren(children);
            }else{
                menuDataEntity.setState(null);
            }
        }else{
            return null;
        }
        return menuDataEntity;
    }
    public static Menu MenuDataConverter(MenuDataEntity dataEntity){
        Menu menu=new Menu();
        if (dataEntity!=null){
            menu.setId(Long.valueOf(dataEntity.getId()));
            menu.setMenuname(dataEntity.getText());
            menu.setMenuicon(dataEntity.getIconCls());
            menu.setMenushow(1);
            menu.setMenusequence(1);
            menu.setMenucode("");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                menu.setAddtime(formatter.parse("2017-004-20 14:00:00"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if(dataEntity.getAttributes()!=null){
                menu.setMenushowtype(dataEntity.getAttributes().getType());
                menu.setMenuaction(dataEntity.getAttributes().getAction());
            }
            if(dataEntity.getChildren()!=null && dataEntity.getChildren().size()>0){
                menu.setMenutype(0);
                List<Menu> children=new ArrayList<Menu>();
                for (MenuDataEntity item:dataEntity.getChildren()){
                    if (item==null)
                        continue;;
                    Menu _item=MenuDataConverter(item);
                    if(_item!=null)
                        children.add(_item);
                }
                menu.setChildren(children);
            }else{
                menu.setMenutype(1);
            }
        }else{
            return null;
        }
        return menu;
    }
    public static List<MenuDataEntity> UserRolePermission(List<MenuDataEntity> source,List<Long> permission){
        List<MenuDataEntity> result=new ArrayList<MenuDataEntity>();
        for (MenuDataEntity data:source) {
            MenuDataEntity _item = new MenuDataEntity();
            BeanUtils.copyProperties(data, _item, ignoreProperties);
            boolean isExitsPermission = false;
            if (data.getChildren() != null && data.getChildren().size() > 0) {
                List<MenuDataEntity> childMenus = UserRolePermission(data.getChildren(), permission);
                if (childMenus.size() > 0) {
                    isExitsPermission = true;
                    _item.setChildren(childMenus);
                }
            }
            if (permission.contains(Long.valueOf(_item.getId()))) {
                isExitsPermission = true;
            }
            if (isExitsPermission)
                result.add(_item);
        }
        return result;
    }
    public static boolean HasModulePermission(long id,List<RoleModule> roleModuleList){
        boolean isResult=false;
        for (RoleModule item:roleModuleList) {
            if(item.getModuleid().equals(Long.valueOf(id))) {
                isResult=true;
                break;
            }
        }
        return isResult;
    }
}
