package com.sptg.gateway.controller.device;

import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.device.api.constant.DeviceSecurityToken;
import com.sptg.device.api.dto.DeviceOptionsDto;
import com.sptg.device.api.dto.TimeStrategyPushTaskDto;
import com.sptg.device.api.entity.*;
import com.sptg.device.api.feigns.*;
import com.sptg.permissions.api.constant.PermissionsSecurityToken;
import com.sptg.permissions.api.dto.AppPackageDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.AppPackage;
import com.sptg.permissions.api.feigns.AppPackageFeign;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * title: DeviceViewController
 * package: com.sptg.gateway.controller.device
 * description: 设备模块-视图控制器
 */
@Controller
@AllArgsConstructor
public class DeviceViewController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final TimeStrategyPushTaskFeign timeStrategyPushTaskFeign;
    private final FlowCardFeign flowCardFeign;
    private final DeviceFeign deviceFeign;
    private final DeviceOptionsFeign deviceOptionsFeign;
    private final DeviceRemoteInfoFeign deviceRemoteInfoFeign;

    @GetMapping(value = "/device/instruct/log")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_MANAGE)
    public ModelAndView instructLog(InstructPublishLog instructPublishLog){
        Assert.notNull(instructPublishLog.getSerialNumber(),"设备序列号不能为空");
        ModelAndView modelAndView = new ModelAndView("device/instruct-publish-log");
        modelAndView.addObject("serialNumber",instructPublishLog.getSerialNumber());
        modelAndView.addObject("xqName",instructPublishLog.getXqName());
        return modelAndView;
    }

    /* 设备管理 */
    /* -------------------------------------------------------------------------------------------------------------- */

    /**
     * 功能描述: 设备信息管理界面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/index")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_MANAGE)
    public ModelAndView deviceManage(@Param("onlineStatus") Integer onlineStatus){
        return new ModelAndView("device/device-manage").addObject("onlineStatus", onlineStatus);
    }

    /**
     * 功能描述: 添加设备页面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/add")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_ADD)
    public ModelAndView addDevice(Long parentId,Integer flag){
        ModelAndView modelAndView = new ModelAndView("device/device-add");
        modelAndView.addObject("parentId",parentId);
        modelAndView.addObject("flag",flag);
        return modelAndView;
    }

    /**
     * 功能描述: 添加设备页面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/relate/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_RELATE)
    public ModelAndView relateResidential(@PathVariable(value = "id")Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-relate");
        Device device = this.deviceFeign.getEntityById(id);
        modelAndView.addObject("device",device);
        return modelAndView;
    }

    /**
     * 功能描述: 修改设备页面
     * @param id 设备Id
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/edit/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_UPDATE)
    public ModelAndView editDevice(@PathVariable(value = "id")Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-edit");
        Device device = this.deviceFeign.getEntityById(id);
        DeviceOptions deviceOptions = this.deviceOptionsFeign.getByDeviceId(id);
        modelAndView.addObject("device",device);
        if (deviceOptions != null){
            modelAndView.addObject("deviceOptions",deviceOptions);
            if (deviceOptions.getFaceOpenDoor() == 1 && deviceOptions.getCardOpenDoor() == 1 && deviceOptions.getRemoteOpenDoor() == 1 && deviceOptions.getPwdOpenDoor() == 1 && deviceOptions.getKeyboard() == 1 && deviceOptions.getAdPlay() == 1){
                modelAndView.addObject("all",1);
            }else if (deviceOptions.getFaceOpenDoor() == 2 && deviceOptions.getCardOpenDoor() == 2 && deviceOptions.getRemoteOpenDoor() == 2 && deviceOptions.getPwdOpenDoor() == 2 && deviceOptions.getKeyboard() == 2 && deviceOptions.getAdPlay() == 2){
                modelAndView.addObject("all",2);
            }else {
                modelAndView.addObject("all",0);
            }
        }
        return modelAndView;
    }
    @GetMapping(value = "/device/manage/complete/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_COMPLETE)
    public ModelAndView completeDevice(@PathVariable(value = "id")Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-complete");
        Device device = this.deviceFeign.getEntityById(id);
        modelAndView.addObject("device",device);
        return modelAndView;
    }

    /**
     * 功能描述: 更换设备页面
     * @param id 设备Id
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/replace/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_UPDATE)
    public ModelAndView replace(@PathVariable(value = "id")Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-replace");
        Device device = this.deviceFeign.getEntityById(id);
        modelAndView.addObject("device",device);
        return modelAndView;
    }

    /**
     * 功能描述: 批量修改页面
     * @param idList 设备Id列表
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/batch/edit")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_BATCH_UPDATE)
    public ModelAndView batchEdit(@RequestParam(value = "idList") List<Long> idList){
        ModelAndView modelAndView = new ModelAndView("device/device-batch-edit");
        modelAndView.addObject("idList",idList);
        return modelAndView;
    }

    /**
     * 功能描述: 部分设备升级版本选择页面
     * deviceFactory 设备厂商名
     * typeCode 设备型号
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/batch/selectUpgrade/{deviceFactory}/{deviceModelNumber}")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_BATCH_UPDATE)
    public ModelAndView selectUpgrade(@PathVariable(value = "deviceFactory") String deviceFactory, @PathVariable(value = "deviceModelNumber") String deviceModelNumber){
        Assert.notNull(deviceFactory,"设备厂商不能为空");
        Assert.notNull(deviceModelNumber,"设备型号不能为空");
        ModelAndView modelAndView = new ModelAndView("device/device-select-version-upgrade");
        modelAndView.addObject("deviceFactory",deviceFactory);
        modelAndView.addObject("deviceModelNumber",deviceModelNumber);
        return modelAndView;
    }

    /**
     * 功能描述: 设备详情页面
     * @param id 设备Id
     * @return ModelAndView
     */
    @GetMapping(value = "/device/manage/info/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_MANAGE)
    public ModelAndView deviceInfo(@PathVariable(value = "id")Long id){
        Device device = this.deviceFeign.getEntityById(id);
        if (device == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("device/device-info");
        if (device.getFlowCardId() != null){
            FlowCard flowCard = this.flowCardFeign.getById(device.getFlowCardId());
            modelAndView.addObject("flowCard",flowCard);
        }
        modelAndView.addObject("id",id);
        modelAndView.addObject("device",device);
        DeviceRemoteInfo deviceRemoteInfo = this.deviceRemoteInfoFeign.getBySerialNumber(device.getSerialNumber());
        if (deviceRemoteInfo != null){
            modelAndView.addObject("deviceRemoteInfo",deviceRemoteInfo);
        }
        return modelAndView;
    }

    /* 设备位置 */
    /* -------------------------------------------------------------------------------------------------------------- */

    /**
     * 功能描述:设备位置页面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/path/manage/index")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_MANAGE)
    public ModelAndView devicePathManage(){
        return new ModelAndView("device/device-path-manage");
    }

    /**
     * 功能描述:添加设备位置页面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/path/add")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_ADD)
    public ModelAndView addDevicePath(Long parentId,Integer flag){
        ModelAndView modelAndView = new ModelAndView("device/device-path-add");
        modelAndView.addObject("parentId",parentId);
        modelAndView.addObject("flag",flag);
        return modelAndView;
    }

    /**
     * 功能描述:修改设备位置页面
     * @param id 主键
     * @return ModelAndView
     */
    @GetMapping(value = "/device/path/edit/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_UPDATE)
    public ModelAndView editDevicePath(@PathVariable(value = "id")Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-path-edit");
        modelAndView.addObject("id",id);
        return modelAndView;
    }

    /**
     * 功能描述:流量卡设置页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/device/flowCard/manage/index")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_MANAGE)
    public ModelAndView deviceFlowCardManage(){
        return new ModelAndView("device/device-flowcard-manage");
    }

    /**
     * 功能描述:添加流量卡页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/device/flowCard/add")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_ADD)
    public ModelAndView addFlowCard(){
        return new ModelAndView("device/device-flowcard-add");
    }

    /**
     * 功能描述:修改流量卡页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/device/flowCard/edit/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_UPDATE)
    public ModelAndView edit(@PathVariable(value = "id")Long id){
        FlowCard flowCard = flowCardFeign.getById(id);
        if (flowCard == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("device/device-flowcard-edit");
        modelAndView.addObject("flowCard",flowCard);
        return modelAndView;
    }

    /**
     * 功能描述:流量卡绑定页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/device/flowCard/bind/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_BIND)
    public ModelAndView bind(@PathVariable(value = "id")Long id){
        FlowCard flowCard = this.flowCardFeign.getById(id);
        if (flowCard == null){
            return new ModelAndView("common/404");
        }

        ModelAndView modelAndView = new ModelAndView("device/device-flowcard-bind");
        modelAndView.addObject("id",id);
        modelAndView.addObject("orgId",flowCard.getOrgId());
        return modelAndView;
    }
    /**
     * 功能描述:设备绑定流量卡页面
     * @return ModelAndView
     *
     *
     */
    @GetMapping(value = "/device/flowCard/device/bind/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_FLOWCARD_MANAGE)
    public ModelAndView FlowCardBind(@PathVariable(value = "id")Long id){
        Device device = this.deviceFeign.getEntityById(id);
        if (device == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("device/flow-card-bind");
        modelAndView.addObject("device",device);
        return modelAndView;
    }

    /**
     * 功能描述:设备升级页面
     */
//    @GetMapping(value = "/device/Upgrade/{id}")
//    @Security(token = PermissionsSecurityToken.DEVICE_APP_PACKAGE_UPGRADE)
//    public ModelAndView deviceUpgrade(@PathVariable(value = "id")Long id){
//        Device device = this.deviceFeign.getEntityById(id);
//        if (StringUtils.isEmpty(device.getDeviceFactory()) || StringUtils.isEmpty(device.getDeviceModelNumber())){
//            return new ModelAndView("common/404");
//        }
//        AppPackageDto appPackageDto = new AppPackageDto();
//        appPackageDto.setDeviceFactory(device.getDeviceFactory());
//        appPackageDto.setDeviceModelNumber(device.getDeviceModelNumber());
//        List<AppPackage> list = appPackageFeign.selectAppPackageByCondition(appPackageDto);
//        ModelAndView modelAndView = new ModelAndView("device/device-version-upgrade");
//        modelAndView.addObject("list",list).addObject("id",id);
//        return modelAndView;
//    }


    /* 队列服务器管理 */
    /* -------------------------------------------------------------------------------------------------------------- */

    /**
     * 功能描述: 队列服务器管理页面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/mq/index")
    @Security(token = DeviceSecurityToken.MQ_SERVER_MANAGE)
    public ModelAndView mqManage(){
        return new ModelAndView("device/mq-manage");
    }

    /**
     * 功能描述: 添加队列服务器
     * @return ModelAndView
     */
    @GetMapping(value = "/device/mq/add")
    @Security(token = DeviceSecurityToken.MQ_SERVER_ADD)
    public ModelAndView addMqServer(){
        return new ModelAndView("device/mq-add");
    }

    /**
     * 功能描述: 编辑队列服务器
     * @return ModelAndView
     */
    @GetMapping(value = "/device/mq/edit/{id}")
    @Security(token = DeviceSecurityToken.MQ_SERVER_UPDATE)
    public ModelAndView editMqServer(@PathVariable(value = "id") Long id){
        ModelAndView modelAndView = new ModelAndView("device/mq-edit");
        modelAndView.addObject("id",id);
        return modelAndView;
    }

    /* 设备策略管理 */
    /* -------------------------------------------------------------------------------------------------------------- */

    /**
     * 功能描述:设备策略管理界面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/time/strategy/manage")
    @Security(token = DeviceSecurityToken.DEVICE_INFO_MANAGE)
    public ModelAndView timeStrategyManage(){
        return new ModelAndView("device/time-strategy-manage");
    }
    /**
     * 功能描述:添加设备策略界面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/time/strategy/add")
    @Security(token = DeviceSecurityToken.TIME_STRATEGY_ADD)
    public ModelAndView addTimeStrategy(){
        return new ModelAndView("device/time-strategy-add");
    }
    /**
     * 功能描述:编辑设备策略界面
     * @param id
     * @return ModelAndView
     */
    @GetMapping(value = "/device/time/strategy/edit/{id}")
    @Security(token = DeviceSecurityToken.TIME_STRATEGY_UPDATE)
    public ModelAndView editTimeStrategy(@PathVariable(value = "id")Long id){
        ModelAndView modelAndView = new ModelAndView("device/time-strategy-edit");
        modelAndView.addObject("id",id);
        return modelAndView;
    }
    /* 设备策略发布管理 */
    /* -------------------------------------------------------------------------------------------------------------- */
    /**
     * 功能描述:设备策略发布管理界面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/time/strategy/push/task/manage")
    @Security(token = DeviceSecurityToken.TIME_STRATEGY_PUSH_MANAGE)
    public ModelAndView timeStrategyPushTaskManage(){
        return new ModelAndView("device/time-strategy-push-task-manage");
    }

    /**
     * 功能描述:设备策略发布页面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/time/strategy/push")
    @Security(token = DeviceSecurityToken.TIME_STRATEGY_PUSH_ADD)
    public ModelAndView timeStrategyPush(){
        return new ModelAndView("device/time-strategy-push");
    }

    /**
     * 功能描述:设备策略发布详细页面
     * @param taskId
     * @return ModelAndView
     */
    @GetMapping(value = "/device/time/strategy/push/detail/{taskId}")
    @Security(token = DeviceSecurityToken.TIME_STRATEGY_MANAGE)
    public ModelAndView pushTimeStrategyDetail(@PathVariable(value = "taskId")Long taskId){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        TimeStrategyPushTaskDto dto = new TimeStrategyPushTaskDto();
        dto.setId(taskId);
        dto.setOrgPosition(onLineUser.getOrgPosition());
        //  获取任务信息
        TimeStrategyPushTask task = this.timeStrategyPushTaskFeign.getEntityById(dto);
        if (null == task){
            return new ModelAndView("common/404");
        }
        //工作日星期
        String getWorkingDay="";
        String workingDay=task.getWorkingDay();
        if(StringUtils.isNotBlank(workingDay)){
           String [] workingArra=workingDay.split(",");
           int i=0;
           for(String weekDay:workingArra){
               if(i>0){
                   getWorkingDay+=",";
               }
               i++;
               switch (weekDay) {
                   case "1":
                       getWorkingDay+="星期一";
                   break;
                   case "2":
                       getWorkingDay+="星期二";
                   break;
                   case "3":
                       getWorkingDay+="星期三";
                   break;
                   case "4":
                       getWorkingDay+= "星期四";
                   break;
                   case "5":
                       getWorkingDay="星期五";
                   break;
                   case "6":
                       getWorkingDay+="星期六";
                   break;
                   case "7":
                       getWorkingDay+="星期七";
                   break;
                   default:
                       getWorkingDay="";
                   break;
               }
           }
            task.setWorkingDay(getWorkingDay);
        }

        ModelAndView modelAndView = new ModelAndView("device/time-strategy-push-detail");
        modelAndView.addObject("task",task);
        return modelAndView;
    }

    /**
     * 功能描述:设备策略发布记录界面
     * @return ModelAndView
     */
    @GetMapping(value = "/device/time/strategy/push/task/record")
    @Security(token = DeviceSecurityToken.TIME_STRATEGY_PUSH_MANAGE)
    public ModelAndView timeStrategyPushTasRecord(){
        return new ModelAndView("device/time-strategy-push-task-record");
    }

}
