package com.ifish.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ifish.entity.CameraUserId;
import com.ifish.entity.Device;
import com.ifish.entity.DeviceUser;
import com.ifish.entity.QuestionsFeedback;
import com.ifish.entity.ShopsInfo;
import com.ifish.entity.User;
import com.ifish.enums.ResultEnum;
import com.ifish.helper.UserHelperI;
import com.ifish.service.BaseService;
import com.ifish.service.UserService;
import com.ifish.util.IfishFilePath;
import com.ifish.util.IfishUtil;

/**
 * @ClassName:UserAction
 * @Description:TODO
 * @author ggw
 */
@RestController
@RequestMapping("/user")
public class UserAction {

    @Autowired
    private UserService userService;
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserHelperI userHelperI;

    private static Logger log = LoggerFactory.getLogger(UserAction.class);

    /**
     * 获取验证码
     *
     * @param sendType
     * @param phoneNumber
     * @return
     */
    @Deprecated
    @RequestMapping("/getSecurityCode.do")
    public Object getSecurityCode(@RequestParam String sendType, @RequestParam String phoneNumber) {
        try {
            return this.baseService.getSecurityCode(phoneNumber, sendType);
        } catch (Exception e) {
            log.error("send sms:phoneNumber:{},error message:{}", phoneNumber, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 通过短信验证后新增注册用户信息
     *
     * @param user
     * @return
     */
    @Deprecated
    @RequestMapping("/addUser.do")
    public Object addUser(User user) {
        try {
            return this.baseService.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("user register:phoneNumber:{},error message:{}", user.getPhoneNumber(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 通过短信验证后修改密码
     *
     * @param user
     * @return
     */
    @RequestMapping("/resetPassword.do")
    public Object resetPassword(User user) {
        try {
            return this.baseService.resetPassword(user);
        } catch (Exception e) {
            log.error("forget password:phoneNumber:{},error message:{}", user.getPhoneNumber(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 用户登录
     */
    @Deprecated
    @RequestMapping("/userLogin.do")
    public Object userLogin(User user) {
        try {
            return userHelperI.login(user);
        } catch (Exception e) {
            log.error("user login:phoneNumber:{},error message:{}", user.getPhoneNumber(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 用户登录
     */
    @Deprecated
    @RequestMapping("/userLoginold.do")
    public Object userLogin1(User user) {
        try {
            return this.baseService.login(user);
        } catch (Exception e) {
            log.error("user login:phoneNumber:{},error message:{}", user.getPhoneNumber(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 验证原密码后修改密码
     *
     * @param oldPassword
     * @param user
     * @return
     */
    @RequestMapping("/updatePwd.do")
    public Object updatePwd(@RequestParam String oldPassword, User user) {
        try {
            return baseService.updatePwd(user, oldPassword);
        } catch (Exception e) {
            log.error("change password:userId:{},error message:{}", user.getUserId(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 问题反馈
     *
     * @param questionsFeedback
     * @return
     */
    @Deprecated
    @RequestMapping("/questionsFeedback.do")
    public Object questionsFeedback(QuestionsFeedback questionsFeedback) {
        try {
            return this.baseService.saveQuestionsFeedback(questionsFeedback);
        } catch (Exception e) {
            log.error("submit questions:userId:{},error message:{}", questionsFeedback.getUserId(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 绑定设备
     *
     * @param user
     * @param macAddress
     * @return
     */
    @Deprecated
    @RequestMapping("/bindDevice.do")
    public Object bindDevice(User user, String macAddress) {
        try {
            return baseService.bindDevice(user, macAddress.toLowerCase());
        } catch (Exception e) {
            log.error("bind device:userId:{},macAddress:{},error message:{}", user.getUserId(), macAddress.toLowerCase(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 绑定摄像头
     *
     * @param user
     * @param macAddress
     * @return
     */
    @RequestMapping("/bindCamera.do")
    public Object bindCamera(Integer userId, String cameraId) {
        return baseService.bindCamera(userId, cameraId);
    }

    /**
     * 关联设备和摄像头
     *
     * @param user
     * @param macAddress
     * @return
     */
    @RequestMapping("/deviceBindCamera.do")
    public Object deviceBindCamera(Integer deviceId, String cameraId) {
        try {
            return baseService.deviceBindCamera(deviceId, cameraId);
        } catch (Exception e) {
            log.error("DeviceBindCamera:cameraId:{},error message:{}", deviceId, cameraId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 关联设备和摄像头1
     *
     * @param user
     * @param macAddress
     * @return
     */
    @RequestMapping("/deviceBindCamera1.do")
    public Object deviceBindCamera1(Integer userId, Integer deviceId, String cameraId) {
        try {
            return baseService.deviceBindCamera1(userId, deviceId, cameraId);
        } catch (Exception e) {
            log.error("DeviceBindCamera1:userId:{},cameraId:{},error message:{}", userId, deviceId, cameraId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 激活摄像头
     *
     * @param user
     * @param macAddress
     * @return
     */
    @RequestMapping("/activeCamera.do")
    public Object activeCamera(String activeCode, String cameraId) {
        try {
            return baseService.activeCamera(activeCode, cameraId);
        } catch (Exception e) {
            log.error("activeCamera:activeCode:{},cameraId:{},error message:{}", activeCode, cameraId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 保存自定义图标
     *
     * @param user
     * @param macAddress
     * @return
     */
    @RequestMapping("/saveCustomIcon.do")
    public Object saveCustomIcon(DeviceUser deviceUser) {
        try {
            return baseService.updateCustomIcon(deviceUser);
        } catch (Exception e) {
            log.error("saveCustomIcon:userId:{},deviceId:{},error message:{}", deviceUser.getPriId().getUserId(), deviceUser.getPriId().getDeviceId(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 获取单个设备信息
     *
     * @param user
     * @return
     */
    @RequestMapping("/getSingleDeviceInf.do")
    public Object getSingleDeviceInf(DeviceUser deviceUser) {
        try {
            return baseService.getSingleDeviceInf(deviceUser);
        } catch (Exception e) {
            log.error("get getDeviceInfByMacAddress Information:userId:{},deviceId:{},error message:{}", deviceUser.getPriId().getUserId(), deviceUser.getPriId().getDeviceId(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 修改设备信息
     *
     * @param deviceUser
     * @return
     */
    @RequestMapping("/updateDeviceUser.do")
    public Object updateDeviceUser(DeviceUser deviceUser) {
        try {
            return baseService.update(deviceUser);
        } catch (Exception e) {
            log.error("update device Information:userId:{},deviceId:{},error message:{}", deviceUser.getPriId().getUserId(), deviceUser.getPriId().getDeviceId(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 修改摄像头信息
     *
     * @param deviceUser
     * @return
     */
    @RequestMapping("/updateCameraUser.do")
    public Object updateCameraUser(Integer userId, String cameraId, String showName) {
        try {
            CameraUserId id = new CameraUserId(userId, cameraId);
            return baseService.update(id, showName);
        } catch (Exception e) {
            log.error("updateCameraUser:userId:{},cameraId:{},error message:{}", userId, cameraId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 删除绑定设备
     *
     * @param deviceUser
     * @return
     */
    @RequestMapping("/deleteDeviceUser.do")
    public Object deleteDeviceUser(DeviceUser deviceUser) {
        try {
            return baseService.deleteDeviceUser(deviceUser);
        } catch (Exception e) {
            log.error("delete the binding device:userId:{},deviceId:{},error message:{}", deviceUser.getPriId().getUserId(), deviceUser.getPriId().getDeviceId(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 删除绑定的摄像头
     *
     * @param deviceUser
     * @return
     */
    @RequestMapping("/deleteCameraUser.do")
    public Object deleteCameraUser(Integer userId, String cameraId) {
        try {
            return baseService.deleteCameraUser(userId, cameraId);
        } catch (Exception e) {
            log.error("delete the binding camera:userId:{},cameraId:{},error message:{}", userId, cameraId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 删除设备和摄像头关联
     *
     * @param deviceUser
     * @return
     */
    @RequestMapping("/deleteDeviceCamera.do")
    public Object deleteDeviceCamera(Integer deviceId, String cameraId) {
        try {
            return baseService.deleteDeviceCamera(deviceId, cameraId);
        } catch (Exception e) {
            log.error("deleteDeviceCamera:deviceId:{},cameraId:{},error message:{}", deviceId, cameraId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 扫一扫分享设备
     *
     * @param user
     * @param device
     * @return
     */
    @Deprecated
    @RequestMapping("/shareDeviceByQrCode.do")
    public Object shareDeviceByQrCode(Integer userId, Integer deviceId) {
        try {
            return baseService.shareDeviceByQrCode(userId, deviceId);
        } catch (Exception e) {
            log.error("share device by qrCode:userId:{},deviceId:{},error message:{}", userId, deviceId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 获取最新版本信息
     */
    @RequestMapping("/getNewestVersion.do")
    public Object getNewest(String phoneType) {
        try {
            return this.baseService.getNewestVersionInf(phoneType.toLowerCase());
        } catch (Exception e) {
            log.error("get the latest version information:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 获取换水提醒信息
     *
     * @param deviceId
     * @return
     */
    @RequestMapping("/getRemindWaterInf.do")
    public Object getRemindWaterInf(Integer deviceId) {
        try {
            return this.baseService.getRemindWaterInf(deviceId);
        } catch (Exception e) {
            log.error("getRemindWaterInf:deviceId:{},error message:{}", deviceId, e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 设置换水提醒信息
     *
     * @param deviceId
     * @return
     */
    @RequestMapping("/setRemindWaterInf.do")
    public Object setRemindWaterInf(Device device) {
        try {
            return this.baseService.setRemindWaterInf(device);
        } catch (Exception e) {
            log.error("setRemindWaterInf:deviceId:{},error message:{}", device.getDeviceId(), e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 保存商户信息
     *
     * @param shopsInfo
     * @param file1
     * @param file2
     * @param file3
     * @return
     */
    @RequestMapping("/saveShopsInfo.do")
    public Object saveShopsInfo(ShopsInfo shopsInfo, MultipartFile file1, MultipartFile file2, MultipartFile file3) {
        try {
            if (file1.getSize() > 3145728 || file2.getSize() > 3145728 || file3.getSize() > 3145728) {
                return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
            } else {
                return this.baseService.saveShopsInfo(shopsInfo, file1, file2, file3);
            }
        } catch (Exception e) {
            log.error("saveShopsInfo:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 审核失败后修改商户信息
     *
     * @param shopsInfo
     * @param file1
     * @param file2
     * @param file3
     * @return
     */
    @RequestMapping("/updateShopsInfo.do")
    public Object updateShopsInfo(ShopsInfo shopsInfo, MultipartFile file1, MultipartFile file2, MultipartFile file3) {
        try {
            if (file1 != null && !file1.isEmpty() && file1.getSize() > 3145728) {
                return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
            }
            if (file2 != null && !file2.isEmpty() && file2.getSize() > 3145728) {
                return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
            }
            if (file3 != null && !file3.isEmpty() && file3.getSize() > 3145728) {
                return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
            } else {
                return this.baseService.updateShopsInfo(shopsInfo, file1, file2, file3);
            }
        } catch (Exception e) {
            log.error("updateShopsInfo:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 审核成功后修改商户信息
     *
     * @param shopsInfo
     * @param file1
     * @param file2
     * @param file3
     * @return
     */
    @RequestMapping("/updateBaseShopsInfo.do")
    public Object updateBaseShopsInfo(ShopsInfo shopsInfo, MultipartFile file4) {
        try {
            if (file4 != null && !file4.isEmpty() && file4.getSize() > 3145728) {
                return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
            } else {
                return this.baseService.updateBaseShopsInfo(shopsInfo, file4);
            }
        } catch (Exception e) {
            log.error("updateBaseShopsInfo:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 查询审核状态
     *
     * @param shopsId
     * @return
     */
    @RequestMapping("/getShopsStatus.do")
    public Object getShopsStatus(Integer shopsId) {
        try {
            return this.baseService.getShopsStatus(shopsId);
        } catch (Exception e) {
            log.error("getShopsStatus:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 选择看护商家
     *
     * @param phoneNumber
     * @param userId
     * @return
     */
    @RequestMapping("/choiceShops.do")
    public Object choiceShops(String phoneNumber, Integer userId) {
        try {
            return this.baseService.choiceShops(phoneNumber, userId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("choiceShops:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 关闭看护摄像头
     *
     * @param cameraId
     * @param userId
     * @param status
     * @return
     */
    @RequestMapping("/onoffLook.do")
    public Object closeLook(String cameraId, Integer userId, String status) {
        try {
            return this.baseService.onoffLook(cameraId, userId, status);
        } catch (Exception e) {
            log.error("closeLook:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 生成微信分享页面
     *
     * @param title
     * @param base64
     * @return
     */
    @RequestMapping("/getHtmlFile.do")
    public Object getHtmlFile(@RequestParam String title, @RequestParam MultipartFile fileUpload) {
        try {
            if (fileUpload != null && fileUpload.getSize() > 3145728) {
                return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
            }
            String pngName = IfishUtil.uploadFile(fileUpload, IfishFilePath.path_share_img);
            if (pngName != null && !pngName.equals("")) {
                String fileName = IfishUtil.getHtmlFile(title, pngName, IfishFilePath.html_name, IfishFilePath.path_share_html);
                if (fileName != null && !fileName.equals("")) {
                    return IfishUtil.returnJson(ResultEnum.success.getKey(), fileName);
                }
            }
        } catch (Exception e) {
            log.error("getHtmlFile:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 生成看护报告
     *
     * @param index1
     * @param index2
     * @param index3
     * @param suggestion
     * @return
     */
    @RequestMapping("/getLookReport.do")
    public Object getLookReport(Integer userId, String index1, String index2, String index3, String suggestion, MultipartFile fileUpload) {
        try {
            if (fileUpload != null && fileUpload.getSize() > 3145728) {
                return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
            }
            return this.baseService.lookReport(userId, index1, index2, index3, suggestion, fileUpload);
        } catch (Exception e) {
            log.error("getLookReport:error message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 发送看护报告给用户(旧)
     *
     * @param userId
     * @param fileName
     * @return
     */
    @Deprecated
    @RequestMapping("/sendReport.do")
    public Object sendReport(Integer userId, String fileName) {
        try {
            return this.baseService.sendReport(userId, fileName);
        } catch (Exception e) {
            log.error("sendReport message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 发送看护报告给用户(新)
     *
     * @param userId
     * @param fileName
     * @param reportId
     * @return
     */
    @RequestMapping("/sendReport1.do")
    public Object sendReport1(Integer userId, String fileName, Integer reportId) {
        try {
            return this.baseService.sendReport(userId, fileName, reportId);
        } catch (Exception e) {
            log.error("sendReport1 message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

    /**
     * 根据ID查询看护报告详情
     *
     * @param reportId
     * @return
     */
    @RequestMapping("/getLookReportById.do")
    public Object getLookReportById(Integer reportId) {
        try {
            return this.baseService.getLookReportById(reportId);
        } catch (Exception e) {
            log.error("getLookReportById message:{}", e.toString());
        }
        return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
    }

}
