package com.tcl.ah.tvcontrol.utils;


import android.util.Log;

import org.apache.commons.codecc.binary.Base64;

public class MqttDataDealUtil {

    private static final String TAG = "MqttDataDealUtil";
    private static final int START_INT = 5;
    private static final int END_INT = 3;

    private static final int START_LEFT = 11;
    private static final int START_RIGHT = 13;
    private static final int END_LEFT = 8;
    private static final int END_RIGHT = 4;


    /**
     * 解密
     *
     * @param valueBase
     * @return
     */
    public static String Base64Decrypt(String valueBase) {

        String data = StringCustomVaryForDecrypt(valueBase);
        Log.i(TAG, "this is TEST Decrypt0:" + data);
        String str = new String(Base64.decodeBase64(data.getBytes()));
        String res = "";
        try {
            if (str.contains(":"))
                res = str.substring(0, str.lastIndexOf(":"));
        } catch (Exception e) {

        }
        Log.i(TAG, "this is TEST Decrypt:" + res);
        return str;
    }

    /**
     * 加密
     *
     * @param data
     * @return
     */
    public static String Base64Encrypt(String data) {

        String str = new String(Base64.encodeBase64(data.getBytes()));
        String valueBase = StringVaryForEncrypt(str);
        Log.i(TAG, "this is Encrypt:" + valueBase);
        return valueBase;
    }

    private static String StringVaryForDecrypt(String valueBase) {
        if (valueBase.length() < 10) return valueBase;
        String startStr = valueBase.substring(0, END_INT);
        String endStr = valueBase.substring(valueBase.length() - START_INT);
        String str = valueBase.substring(END_INT, valueBase.length() - START_INT);
        String resStr = endStr + str + startStr;

        return resStr;
    }

    //substring[0-3) 0,1,2
    private static String StringCustomVaryForDecrypt(String valueBase) {
        //String valueBase = "bbbbb456*********aaaaa123";
        if (valueBase.length() < 10) return valueBase;
        int strLong = valueBase.length();
        StringBuilder stb = new StringBuilder(valueBase);
        String startStr = valueBase.substring(START_LEFT - 1, START_RIGHT);//
        String endStr = valueBase.substring(strLong - END_LEFT, strLong - END_RIGHT + 1);

        stb.replace(START_LEFT - 1, START_RIGHT, "");
        int startLength = START_RIGHT - START_LEFT;
        System.out.println("this is :" + stb.toString());
        stb.replace(strLong - END_LEFT - startLength - 1, strLong - END_RIGHT - startLength, "");
        System.out.println("this is :" + stb.toString());
        String str = stb.toString();
        String resStr = endStr + str + startStr;

        System.out.println("this is :" + "startStr:" + startStr + "endStr" + endStr + "str:" + str + "resStr:" + resStr);

        return resStr;
    }

    private static String StringVaryForEncrypt(String valueBase) {
        if (valueBase.length() < 10) return valueBase;
        String startStr = valueBase.substring(0, START_INT);
        String endStr = valueBase.substring(valueBase.length() - END_INT);
        String str = valueBase.substring(START_INT, valueBase.length() - END_INT);
        String resStr = endStr + str + startStr;

        return resStr;
    }
}
