package com.fdkankan.scene.controller;

import com.fdkankan.common.constant.ConstantFilePath;
import com.fdkankan.common.util.MatrixToImageWriterUtil;
import com.fdkankan.platform.api.feign.PlatformClient;
import com.fdkankan.platform.api.vo.User;
import com.fdkankan.redis.util.RedisUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/device")
public class TestController {

    @Autowired
    PlatformClient platformClient;

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/test")
    public String test() throws Exception {

//        platformClient.getCameraByChildName()
        MatrixToImageWriterUtil.createQRCode("http://baidu.com" + "123123", ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+"123123"+".png",
                null);
        return null;

    }

    public static void main(String[] args) throws Exception {
        MatrixToImageWriterUtil.createQRCode("http://baidu.com" + "123123", ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+"123123"+".png",
                null);

//        MatrixToImageWriterUtil.createQRCode("http://baidu.com" + "123123", ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+"123123"+".png",
//                ConstantFilePath.SCENE_PATH + "images/images" + "123123" + "/QRShareLogo.png");
    }

    @GetMapping("/testSetStr")
    public boolean testSetStr() throws Exception {
        return redisUtil.set("testSetStr", "123123123123aaaa");
    }

    @GetMapping("/testGetStr")
    public String testGetStr() throws Exception {
        return (String)redisUtil.get("testSetStr");
    }

    @GetMapping("/testSetObject")
    public boolean testSetObject() throws Exception {
        User user = new User();
        user.setCity("珠海");
        user.setId(123l);
        return redisUtil.set("testSetObject", user);
    }

    @GetMapping("/testGetObject")
    public User testGetObject() throws Exception {
        User user = new User();
        user.setCity("珠海");
        user.setId(123l);
        return (User)redisUtil.get("testSetObject");
    }

    @GetMapping("/testSetList")
    public boolean testSetList() throws Exception {

        List<User> users = Lists.newArrayList();
        User user1 = new User();
        user1.setCity("珠海");
        user1.setId(123l);

        User user2 = new User();
        user2.setCity("广州");
        user2.setId(124l);

        users.add(user1);
        users.add(user2);


        return redisUtil.lRightPushAll("testList1", users);
    }

    @GetMapping("/testGetList")
    public List<User> testGetList() throws Exception {
//        List<User> testList = (List<User>) redisUtil.lGet("testList1", 0, -1);
        return redisUtil.lGet("testList1", 0, -1);
    }

    @GetMapping("/testSetMap")
    public boolean testSetMap() throws Exception {

        Map<String, User> userMap = new HashMap<>();

        User user1 = new User();
        user1.setCity("珠海");
        user1.setId(123l);

        User user2 = new User();
        user2.setCity("广州");
        user2.setId(124l);

        userMap.put("user1", user1);
        userMap.put("user2", user2);


        return redisUtil.hmset("userMap", userMap);
    }

    @GetMapping("/testGetMap")
    public Map<String ,User> testGetMap() throws Exception {
        return redisUtil.hmget("userMap");
    }

}
