package com.yutou.common.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;


public class RedisTools {
    public static final int QQBOT_USER = 3;
    public static final String BILI_USER_BUVID = "bili_user_buvid";
    private static boolean isNotInstallRedis = false;
    private static String host;
    private static int port;
    public static int TOKEN_TIMEOUT_DEFAULT = 360;

    private RedisTools() {

    }

    // 写成静态代码块形式，只加载一次，节省资源
    static {
        //Properties properties = PropertyUtil.loadProperties("jedis.properties");
        //host = properties.getProperty("redis.host");
        //port = Integer.valueOf(properties.getProperty("redis.port"));
        host = "172.21.35.118";
        port = 6379;
    }

    public static boolean set(int dbIndex, String key, String value) {
        try {
            if (isNotInstallRedis) {
                return false;
            }
            Jedis jedis = getRedis();
            jedis.select(dbIndex);
            String ret = jedis.set(key, value);
            jedis.close();
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(e);
            return false;
        }
        return true;
    }

    public static boolean set(Object key, String value) {
        return set(0, key.toString(), value);
    }

    /**
     *
     * @param key key
     * @param value value
     * @param timeout 过期时间,单位秒
     */
    public static boolean set(String key, String value, int timeout) {
        try {
            if (isNotInstallRedis) {
                return false;
            }
            Jedis jedis = getRedis();
            if (timeout == -1) {
                jedis.set(key, value);
            } else {
                jedis.setex(key, timeout, value);
            }
            jedis.close();
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(e);
            return false;
        }
        return true;
    }

    public static String get(String key, int dbIndex) {
        String value = null;
        if (isNotInstallRedis) {
            return value;
        }
        try (Jedis jedis = getRedis()) {
            jedis.select(dbIndex);
            value = jedis.get(key);
            jedis.close();
        } catch (Exception e) {
            // TODO: handle exception
            //	Log.e(e);
        }
        return value;
    }

    public static String get(Object key) {
        return get(key.toString(), 0);
    }

    public static boolean remove(Object key) {
       return remove(key.toString(),0);
    }

    public static void removeLoginState(String uid) {
        Jedis jedis = getRedis();
        Set<String> keys = jedis.keys("*");
        for (String string : keys) {
            if (string.equals(uid)) {
                jedis.del(string);
            }
        }

    }

    public static String ping() {
        Jedis jedis = getRedis();
        String tmp = jedis.ping();
        jedis.close();
        return tmp;
    }

    public static boolean exists(Object key, String value) {
        if (isNotInstallRedis) {
            return false;
        }
        Jedis jedis = getRedis();
        boolean flag = value.equals(jedis.get(key.toString()));
        jedis.close();
        return flag;
    }

    public static Jedis getRedis() {
        return new Jedis(host, port);
    }

    public static boolean remove(String key, int index) {
        if (isNotInstallRedis) {
            return false;
        }
        Jedis jedis = getRedis();
        jedis.select(index);
        Long i = jedis.del(key);
        jedis.close();
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    private static class PropertyUtil {

        // 加载property文件到io流里面
        public static Properties loadProperties(String propertyFile) {
            Properties properties = new Properties();
            try {
                InputStream is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
                if (is == null) {
                    is = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFile);
                }
                properties.load(is);
            } catch (IOException e) {
                Log.e(e);
            }
            return properties;
        }
    }

    private static Jedis getPoolRedis() {
        if (isNotInstallRedis) {
            return null;
        }
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(0);
        poolConfig.setMaxWaitMillis(1000);
        JedisPool pool = new JedisPool(poolConfig, host);
        return pool.getResource();
    }

    public static void pullMsg(String channel, String msg) {
        Jedis jedis = getPoolRedis();
        jedis.publish(channel, msg);
        jedis.close();
    }

    private static boolean init = false;


    public static void main(String[] args) {
        RedisTools.pullMsg("msg", "abc");
    }
}
