package com.wayne.www.waynelib;

import com.wayne.www.waynelib.common.Tuple;
import com.wayne.www.waynelib.fdc.MessageRouterClient;
import com.wayne.www.waynelib.util.Util;

import org.junit.Test;
import org.simpleframework.xml.Root;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by Think on 4/20/2016.
 */
@Root
public class UtilTest {
    @Test
    public void doubleTo2DigitsString_test() throws Exception {
        double input = 181595.21d;
        DecimalFormat myFormatter = new DecimalFormat("##.##");
        //myFormatter.setRoundingMode(RoundingMode.DOWN);
        String actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181595.21"));

        input = 181593.72d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.72"));

        input = 181593.73d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.73"));

        input = 181593.74d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.74"));

        input = 181593.75d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.75"));

        input = 181593.76d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.76"));

        input = 181593.77d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.77"));

        input = 181593.78d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.78"));

        input = 181593.79d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.79"));

        input = 181593.70d;
        actual = myFormatter.format(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.7"));
    }

    @Test
    public void floatTo2DigitsString_test1() throws Exception {
        float input = 181595.21f;
        String actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181595.21"));

        input = 181593.72f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.72"));

        input = 181593.73f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.73"));

        input = 181593.74f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.74"));

        input = 181593.75f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.75"));

        input = 181593.76f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.76"));

        input = 181593.77f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.77"));

        input = 181593.78f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.78"));

        input = 181593.79f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.79"));

        input = 181593.70f;
        actual = String.format ("%.2f", input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.70"));
    }

    @Test
    public void floatTo2DigitsString_test2() throws Exception {
        float input = 181593.21f;
        String actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.21"));

        input = 181593.72f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.72"));

        input = 181593.73f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.73"));

        input = 181593.74f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.74"));

        input = 181593.75f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.75"));

        input = 181593.76f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.76"));

        input = 181593.77f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.77"));

        input = 181593.78f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.78"));

        input = 181593.79f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.79"));

        input = 181593.70f;
        actual = Float.toString(input);
        assertTrue(actual, actual.equalsIgnoreCase("181593.70"));
    }

    @Test
    public void date_to_xmlDateString() throws Exception {
        String actual = Util.getNowTimeWithXmlFormat();
        assertTrue(actual, actual != null);
    }

    @Test
    public void convertTimeStrToJavaDate0() throws Exception {
        String rawFromWebService = "1983-10-04T08:18:28";
        Calendar expected = new GregorianCalendar(1983, 10, 4, 8, 18, 28);
        Calendar actual = Util.convertTimeStrToJavaDate(rawFromWebService, "yyyy-MM-dd'T'HH:mm:ss");
        int actualYear = actual.get(Calendar.YEAR);
        int actualMonth = actual.get(Calendar.MONTH) + 1;
        int actualDay = actual.get(Calendar.DAY_OF_MONTH);
        int actualHour = actual.get(Calendar.HOUR_OF_DAY);
        int actualMin = actual.get(Calendar.MINUTE);
        int actualSec = actual.get(Calendar.SECOND);
        assertTrue(actualYear == 1983);
        assertTrue(actualMonth == 10);
        assertTrue(actualDay == 4);
        assertTrue(actualHour == 8);
        assertTrue(actualMin == 18);
        assertTrue(actualSec == 28);
    }

    @Test
    public void convertTimeStrToJavaDate1() throws Exception {
        String rawFromWebService = "1983-10-04";
        Calendar actual = Util.convertTimeStrToJavaDate(rawFromWebService, "yyyy-MM-dd");
        int actualYear = actual.get(Calendar.YEAR);
        int actualMonth = actual.get(Calendar.MONTH) + 1;
        int actualDay = actual.get(Calendar.DAY_OF_MONTH);
        int actualHour = actual.get(Calendar.HOUR_OF_DAY);
        int actualMin = actual.get(Calendar.MINUTE);
        int actualSec = actual.get(Calendar.SECOND);
        assertTrue(actualYear == 1983);
        assertTrue(actualMonth == 10);
        assertTrue(actualDay == 4);
    }

    @Test
    public void convertTimeStrToJavaDate3() throws Exception {
        String rawFromWebService = "1983-10-04 12:01:02";
        Calendar actual = Util.convertTimeStrToJavaDate(rawFromWebService, "yyyy-MM-dd HH:mm:ss");
        int actualYear = actual.get(Calendar.YEAR);
        int actualMonth = actual.get(Calendar.MONTH) + 1;
        int actualDay = actual.get(Calendar.DAY_OF_MONTH);
        int actualHour = actual.get(Calendar.HOUR_OF_DAY);
        int actualMin = actual.get(Calendar.MINUTE);
        int actualSec = actual.get(Calendar.SECOND);
        assertTrue(actualYear == 1983);
        assertTrue(actualMonth == 10);
        assertTrue(actualDay == 4);
        assertTrue(actualHour == 12);
        assertTrue(actualMin == 1);
        assertTrue(actualSec == 2);
    }

    @Test
    public void convertTimeStrToJavaDate4() throws Exception {
        String rawFromWebService = "0001-01-01T00:00:00";
        Calendar actual = Util.convertTimeStrToJavaDate(rawFromWebService, "yyyy-MM-dd'T'HH:mm:ss");
        int actualYear = actual.get(Calendar.YEAR);
        int actualMonth = actual.get(Calendar.MONTH) + 1;
        int actualDay = actual.get(Calendar.DAY_OF_MONTH);
        int actualHour = actual.get(Calendar.HOUR_OF_DAY);
        int actualMin = actual.get(Calendar.MINUTE);
        int actualSec = actual.get(Calendar.SECOND);
        assertTrue(actualYear == 1983);
        assertTrue(actualMonth == 10);
        assertTrue(actualDay == 4);
        assertTrue(actualHour == 12);
        assertTrue(actualMin == 1);
        assertTrue(actualSec == 2);
    }


    @Test
    public void pad_String_test() throws Exception {
        String target = "1234";
        String expectedLeft = "00001234";
        String actualLeft = Util.pad(target, "left", 8, '0');
        assertTrue("failed for left padding, actual: " + actualLeft, expectedLeft.equals(actualLeft));

        String expectedRight = "12340000";
        String actualRight = Util.pad(target, "right", 8, '0');
        assertTrue("failed for left padding, actual: " + actualRight, expectedRight.equals(actualRight));
    }

    @Test
    public void msgRouter_test0() throws Exception {
        Tuple<Boolean, MessageRouterClient.MessageRouterClientState> state = Tuple.create(true, null);
        MessageRouterClient.getDefault().addOnMsgRouterClientStateChangedListeners((a, b) -> {
            state.second = b;
            state.first = false;
        });
        MessageRouterClient.getDefault().start("127.0.0.1");
        while (state.first) {
            Thread.sleep(1000);
        }

        assertTrue(state.second == MessageRouterClient.MessageRouterClientState.Connected);
    }

    @Test
    public void encode_utf8_test() throws UnsupportedEncodingException {
        String actual = Util.encodeUtf8("http://10.98.196.66:8211/api/media/view/0/WeChat Image_20181115164551.jpg?buId=2d41a66f-6b89-4af9-be8a-d076ad62bc0a");
        String expected = "http%3A%2F%2F10.98.196.66%3A8211%2Fapi%2Fmedia%2Fview%2F0%2FWeChat+Image_20181115164551.jpg%3FbuId%3D2d41a66f-6b89-4af9-be8a-d076ad62bc0a";
        assertEquals(actual, expected);
    }

    @Test
    public void decode_utf8_test() throws UnsupportedEncodingException {
        String actual = Util.decodeUtf8("http://10.98.196.66:8211/api%2Fmedia%2Fview%2F2%2FWeChat+Image_20181115164551.jpg%3FbuId%3D2d41a66f-6b89-4af9-be8a-d076ad62bc0a");
        String expected = "http://10.98.196.66:8211/api/media/view/2/WeChat Image_20181115164551.jpg?buId=2d41a66f-6b89-4af9-be8a-d076ad62bc0a";
        assertEquals(actual, expected);
    }

//    @Test
//    public void download_file_by_url() throws InterruptedException {
//        Util.downloadFile();
//        Thread.sleep(1 * 60 * 1000);
//    }
}
