package com.sptg.gateway.controller.permissions;

import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.estate.api.constant.EstateSecurityToken;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sptg.common.core.constant.CommonConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.constant.ConfTokenName;
import com.sptg.permissions.api.constant.PermissionsSecurityToken;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.AppPackage;
import com.sptg.permissions.api.entity.Conf;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.AppPackageFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;

import lombok.AllArgsConstructor;

/**
 * title: PermissionsViewController
 * package: com.sptg.gateway.controller.permissions
 * description: 视图-控制器
 *
 *
 *
 */
@Controller
@AllArgsConstructor
public class PermissionsViewController {

    /* ----------------------------------------------- 接口/参数 注入 ----------------------------------------------- */

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final OrgFeign orgFeign;
    private final AppPackageFeign appPackageFeign;

    /* -------------------------------------------------- 账号管理 -------------------------------------------------- */

    /**
     * @return 账户设置页面
     */
    @Security
    @GetMapping(value = "/permissions/account/setup")
    public ModelAndView setup(){
        ModelAndView modelAndView = new ModelAndView("permissions/account-setup");
        modelAndView.addObject("fastDfsAccessPath",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述: 账号管理页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/account/index")
    @Security(token = PermissionsSecurityToken.ACCOUNT_MANAGE)
    public ModelAndView accountManage(){
        ModelAndView modelAndView = new ModelAndView("permissions/account-manage");
        modelAndView.addObject("fastDfsAccessPath",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述: 创建账户页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/account/add/view")
    @Security(token = PermissionsSecurityToken.ACCOUNT_INSERT)
    public ModelAndView addAccount(){
        Conf conf = this.confFeign.getEntityByToken(ConfTokenName.DEFAULT_PORTRAIT);
        if (null == conf){
            return new ModelAndView("common/500");
        }
        ModelAndView modelAndView = new ModelAndView("permissions/account-add");
        modelAndView.addObject("fastDfsAccessPath",this.confFeign.getFileServerHost());
        modelAndView.addObject("defaultPortrait",conf.getConfValue());
        return modelAndView;
    }

    /**
     * 功能描述: 修改账号页面
     * @param id 账号Id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/account/edit/{id}")
    @Security(token = PermissionsSecurityToken.ACCOUNT_UPDATE)
    public ModelAndView editAccount(@PathVariable(value = "id") Long id){
        ModelAndView modelAndView = new ModelAndView("permissions/account-edit");
        modelAndView.addObject("id",id);
        modelAndView.addObject("fastDfsAccessPath",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /* -------------------------------------------------- 角色管理 -------------------------------------------------- */

    /**
     * 功能描述: 角色管理页面
     *
     *
     */
    @GetMapping(value = "/permissions/role/index")
    @Security(token = PermissionsSecurityToken.ROLE_MANAGE)
    public ModelAndView roleManage(){
        return new ModelAndView("permissions/role-manage");
    }

    /**
     * 功能描述: 新增角色页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/role/add/view")
    @Security(token = PermissionsSecurityToken.ROLE_INSERT)
    public ModelAndView addRole(){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        //  获取用户机构信息
        Organization organization = this.orgFeign.getOrgById(onLineUser.getOrgId());
        if (null == organization) {
            return new ModelAndView(CommonConstant.REDIRECT_LOGOUT);
        }
        ModelAndView modelAndView = new ModelAndView("permissions/role-add");
        modelAndView.addObject("organization",organization);
        return modelAndView;
    }

    /**
     * 功能描述: 编辑角色页面
     * @param id 角色Id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/role/edit/{id}")
    @Security(token = PermissionsSecurityToken.ROLE_UPDATE)
    public ModelAndView updateRole(@PathVariable(value = "id") Long id){
        ModelAndView modelAndView = new ModelAndView("permissions/role-edit");
        modelAndView.addObject("id",id);
        return modelAndView;
    }

    /**
     * 功能描述: 角色绑定账号页面（查看角色已经绑定到哪些账号上,为其解除绑定）
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/role/account/{roleId}")
    @Security(token = PermissionsSecurityToken.ROLE_DISTRIBUTION)
    public ModelAndView roleBindAccountView(@PathVariable(value = "roleId")Long roleId){
        ModelAndView modelAndView = new ModelAndView("permissions/rold-bind");
        modelAndView.addObject("roleId",roleId);
        return modelAndView;
    }

    /**
     * 功能描述: 角色绑定账号页面（查看哪些账号还没有绑定此角色,为其绑定）
     * @param roleId 角色Id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/role/account/bind/user/{roleId}")
    @Security(token = PermissionsSecurityToken.ROLE_DISTRIBUTION)
    public ModelAndView roleBindNewAccountView(@PathVariable(value = "roleId")Long roleId){
        ModelAndView modelAndView = new ModelAndView("permissions/role-bind-account");
        modelAndView.addObject("roleId",roleId);
        return modelAndView;
    }

    /* -------------------------------------------------- 机构管理 -------------------------------------------------- */

    /**
     * 功能描述:机构管理页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping("/permissions/org/index")
    @Security(token = PermissionsSecurityToken.ORGANIZATION_MANAGE)
    public ModelAndView orgManage(){
        return new ModelAndView("permissions/org-manage");
    }

    /**
     * 功能描述:导入机构
     * @param flag 7 机构
     * @return ModelAndView
     *
     *
     */
    @RequestMapping(value = "/permissions/org/indexImportExcel/{flag}")
    @Security(token = PermissionsSecurityToken.ORGANIZATION_INSERT)
    public ModelAndView indexImportExcel(@PathVariable(value = "flag")String flag) {
        if (null == flag){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("common/import-excel");
        modelAndView.addObject("flag",flag);
        return modelAndView;
    }
    /**
     * 功能描述: 添加机构页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/org/add")
    @Security(token = PermissionsSecurityToken.ORGANIZATION_INSERT)
    public ModelAndView addOrg(){
        //  获取当前在线用户信息
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        Organization userOrg = this.orgFeign.getOrgById(onLineUser.getOrgId());
        ModelAndView modelAndView = new ModelAndView("permissions/org-add");
        modelAndView.addObject("userOrg",userOrg);
        return modelAndView;
    }

    /**
     * 功能描述: 修改机构页面
     * @param orgId 机构Id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/org/edit/{orgId}")
    @Security(token = PermissionsSecurityToken.ORGANIZATION_UPDATE)
    public ModelAndView editOrg(@PathVariable(value = "orgId") Long orgId){
        ModelAndView modelAndView = new ModelAndView("permissions/org-edit");
        modelAndView.addObject("orgId",orgId);
        return modelAndView;
    }

    /**
     * 功能描述: 机构迁移页面
     * @param orgId 机构Id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/org/transfer/{orgId}")
    @Security(token = PermissionsSecurityToken.ORGANIZATION_TRANSFER)
    public ModelAndView orgTransfer(@PathVariable(value = "orgId")Long orgId){
        ModelAndView modelAndView = new ModelAndView("permissions/org-transfer");
        modelAndView.addObject("orgId",orgId);
        return modelAndView;
    }

    /**
     * 功能描述: 区域权限详情页
     * @param orgId 机构Id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/org/region/permissions/info/{orgId}")
    @Security(token = PermissionsSecurityToken.ORGANIZATION_REGION_PERMISSIONS)
    public ModelAndView regionPermissions(@PathVariable(value = "orgId")Long orgId){
        ModelAndView modelAndView = new ModelAndView("permissions/region-permissions");
        modelAndView.addObject("orgId",orgId);
        return modelAndView;
    }

    /**
     * 功能描述: 区域权限编辑页面
     * @param orgId 机构Id
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/org/region/permissions/edit/{orgId}")
    @Security(token = PermissionsSecurityToken.ORGANIZATION_REGION_PERMISSIONS)
    public ModelAndView editRegionPermissions(@PathVariable(value = "orgId")Long orgId){
        ModelAndView modelAndView = new ModelAndView("permissions/region-permissions-edit");
        modelAndView.addObject("orgId",orgId);
        return modelAndView;
    }

    /* -------------------------------------------------- 菜单管理 -------------------------------------------------- */

    /**
     * 功能描述: 菜单管理页面
     * 需要当前登陆用户拥有操作的菜单的权限，才可以访问
     * @return ModelAndView
     *
     * date:  2018/11/2 0:35
     */
    @GetMapping(value = "/permissions/menu/index")
    @Security(token = PermissionsSecurityToken.MENU_MANAGE)
    public ModelAndView menuManage(){
        return new ModelAndView("permissions/menu-manage");
    }


    @GetMapping(value = "/permissions/dict/index")
    @Security(token = PermissionsSecurityToken.DICT_MANAGE)
    public ModelAndView dictManage(){
        return new ModelAndView("permissions/dict-manage");
    }

    /* 版本升级 */
    /* -------------------------------------------------------------------------------------------------------------- */

    /**
     * 功能描述:门禁设备APP升级页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/device/appPackage/index")
    @Security(token = PermissionsSecurityToken.DEVICE_APP_PACKAGE)
    public ModelAndView deviceAppPackageManage(){
        return new ModelAndView("device/device-app-package-manage");
    }

    /**
     * 功能描述:门禁设备APP升级添加页面
     *
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/device/appPackage/add")
    @Security(token = PermissionsSecurityToken.DEVICE_APP_PACKAGE)
    public ModelAndView deviceAppPackageAdd(){
        ModelAndView modelAndView = new ModelAndView("device/device-app-package-add");
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:门禁设备APP升级修改页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/device/appPackage/edit/{id}")
    @Security(token = PermissionsSecurityToken.DEVICE_APP_PACKAGE)
    public ModelAndView deviceAppPackageEdit(@PathVariable(value = "id") Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-app-package-edit");
        AppPackage appPackage = this.appPackageFeign.getEntity(id);
        modelAndView.addObject("bean", appPackage);
        if(appPackage!=null){
            String fileName="";
            String downloadUrl = appPackage.getDownloadUrl();
            if(StringUtils.isNotBlank(downloadUrl)){
                int index = downloadUrl.lastIndexOf("/");
                if(index>-1){
                    fileName=downloadUrl.substring(index+1);
                }
            }
            modelAndView.addObject("fileName", fileName);
        }
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:设备管理系机APP升级页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/device/admin/appPackage/index")
    @Security(token = PermissionsSecurityToken.DEVICE_ADMIN_APP_PACKAGE)
    public ModelAndView deviceAdminAppPackageManage(){
        return new ModelAndView("device/device-admin-app-package-manage");
    }

    /**
     * 功能描述:设备管理系机/手机端APP升级添加页面
     * @param deviceType 设备类型
     * @return org.springframework.web.servlet.ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/device/admin/appPackage/add")
    @Security(token = PermissionsSecurityToken.DEVICE_APP_PACKAGE_ADD)
    public ModelAndView deviceAdminAppPackageAdd(Integer deviceType){
        ModelAndView modelAndView;
        if (deviceType.equals(OperationConstant.ONE)){
            modelAndView = new ModelAndView("device/device-mobile-app-package-add");
        }else {
            modelAndView = new ModelAndView("device/device-admin-app-package-add");
        }
        //设备类型
        modelAndView.addObject("deviceType", deviceType).addObject("app","app");
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        modelAndView.addObject("iosAppUrl",this.confFeign.getEntityByToken("ios.app.url").getConfValue());
        return modelAndView;
    }

    /**
     * 功能描述:设备管理系机/手机端 APP升级修改页
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/device/admin/appPackage/edit/{id}")
    @Security(token = PermissionsSecurityToken.DEVICE_APP_PACKAGE_EDIT)
    public ModelAndView deviceAdminAppPackageEdit(@PathVariable(value = "id") Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-admin-app-package-edit");
        AppPackage appPackage = this.appPackageFeign.getEntity(id);
        modelAndView.addObject("bean", appPackage);
        String fileName="";
        if(appPackage!=null){
            String downloadUrl = appPackage.getDownloadUrl();
            int index = downloadUrl.lastIndexOf("/");
            if(index>-1){
                fileName=downloadUrl.substring(index);
            }
            modelAndView.addObject("fileName", fileName);
        }
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return modelAndView;
    }

    /**
     * 功能描述:手机APP升级页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/permissions/device/mobile/appPackage/index")
    @Security(token = PermissionsSecurityToken.DEVICE_MOBILE_APP_PACKAGE)
    public ModelAndView deviceMobileAppPackageManage(){
        return new ModelAndView("device/device-mobile-app-package-manage");
    }
}
