/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.smartapplication.vcdevice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.smartapplication.vcdevice.beans.VideoCloudResult;
import org.springblade.smartapplication.vcdevice.conf.VideoCloudConfig;
import org.springblade.smartapplication.vcdevice.entity.VideoCloudAccount;
import org.springblade.smartapplication.vcdevice.entity.VideoCloudDevice;
import org.springblade.smartapplication.vcdevice.util.VideoCloudUtil;
import org.springblade.smartapplication.vcdevice.vo.VideoCloudAccountVO;
import org.springblade.smartapplication.vcdevice.mapper.VideoCloudAccountMapper;
import org.springblade.smartapplication.vcdevice.service.IVideoCloudAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-12-08
 */
@Service
@Slf4j
public class VideoCloudAccountServiceImpl extends ServiceImpl<VideoCloudAccountMapper, VideoCloudAccount> implements IVideoCloudAccountService {

	@Autowired
	private VideoCloudConfig videoCloudConfig;

	@Override
	public IPage<VideoCloudAccountVO> selectVideoCloudAccountPage(IPage<VideoCloudAccountVO> page, VideoCloudAccountVO videoCloudAccount) {
		return page.setRecords(baseMapper.selectVideoCloudAccountPage(page, videoCloudAccount));
	}

	@Override
	public VideoCloudAccount getByOldId(String oldId) {
		QueryWrapper<VideoCloudAccount> wrapper = new QueryWrapper<>();
		wrapper.eq("old_id", oldId);
		return this.baseMapper.selectOne(wrapper);
	}

	@Override
	public R login(String accountName) {
		VideoCloudAccount videoCloudAccount = this.getByAccountName(accountName);
		Assert.notNull(videoCloudAccount, "视频云账号不存在");
		String aesPassword = VideoCloudUtil.getAESPassword(videoCloudAccount.getPwd(), videoCloudConfig.getPwdEncrypt());
		Map<String, Object> map = VideoCloudUtil.getSysParams(videoCloudAccount.getAppKey(), videoCloudConfig.getFormat(), "login", videoCloudConfig.getSignMethod(), videoCloudConfig.getV(), null);
		map.put("account", videoCloudAccount.getAccountName());
		map.put("password", aesPassword);
		String sign = VideoCloudUtil.getSign(map, videoCloudConfig.getSecret());
		map.put("sign", sign);
		String urlParamsLink = VideoCloudUtil.getUrlParamsLink(map, "&");
		String url = videoCloudConfig.getServerIp() + ":" + videoCloudConfig.getServerPort() + "/api/v2/login"  + "?" + urlParamsLink;
		log.info("=================视频云登录请求数据【" + videoCloudAccount.getAccountName() + "】=====================");
		log.info(url);
		String response = HttpUtil.post(url, new String());
		VideoCloudResult videoCloudResult = JsonUtil.parse(response, VideoCloudResult.class);
		log.info("=================视频云登录响应数据【" + videoCloudAccount.getAccountName() + "】=====================");
		log.info(JsonUtil.toJson(videoCloudResult));
		String token = null;
		if(videoCloudResult.getReturnCode().equals("1")){
			token = videoCloudResult.getToken();
			VideoCloudAccount source = videoCloudResult.getAccount();
			VideoCloudAccount target = null;
			target = baseMapper.selectOne(new QueryWrapper<>(new VideoCloudAccount()).lambda().eq(VideoCloudAccount::getAccountName, source.getAccountName()));
			if(target == null){
				target = new VideoCloudAccount();
			}else{
				source.setId(target.getId());
			}
			BeanUtil.copyProperties(source, target);
			target.setToken(token);
			this.saveOrUpdate(target);
			return R.success("登录成功");
		}else{
			return R.fail(videoCloudResult.getMsg() + ":" + videoCloudResult.getSubMsg());
		}
	}

	@Override
	public VideoCloudAccount getByAccountName(String accountName) {
		LambdaQueryWrapper<VideoCloudAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(VideoCloudAccount::getAccountName, accountName);
		return this.getOne(lambdaQueryWrapper);
	}

}
