package helper;

import com.yanzuoguang.util.MediaHelperExtend;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.thread.RunnableListAuto;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestMediaHelper {

    private boolean isFirstImage = false;
    public float[] sizes = new float[]{1f, 0.5f, 1f, 1f, 0.5f, 0.5f};
    public float[] quotes = new float[]{1f, 1f, 0.5f, 0.25f, 0.5f, 0.25f};


    @Test
    public void testMediaType() throws EncoderException, IOException {
        System.out.println("100M.mp4   " + FileHelper.getMediaType(getFile("100M.mp4")));
        System.out.println("100M.mp4.jpg  " + FileHelper.getMediaType(getFile("100M.mp4.jpg")));
        System.out.println("xuziming.mp4  " + FileHelper.getMediaType(getFile("xuziming.mp4")));
        System.out.println("z001594372388232a3017ad69c82a342.MOV  " + FileHelper.getMediaType(getFile("z001594372388232a3017ad69c82a342.MOV")));
    }

    @Test
    public void printEncoding() throws EncoderException {
        Encoder encoder = new Encoder();
        String[] audioEncoders = encoder.getVideoEncoders();
        for (String item : audioEncoders) {
            System.out.println(item);
        }
    }


    @Test
    public void testConvert() throws EncoderException {
        Encoder encoder = new Encoder();
        String[] audioEncoders = encoder.getVideoEncoders();
        List<Runnable> list = new ArrayList<>();
        for (String item : audioEncoders) {
            list.add(testConvertThread(item, 1f, 1f));
        }
        RunnableListAuto.run(list);
    }

    private Runnable testConvertThread(String item, float size, float quote) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    testConvert(item, size, quote);
                } catch (EncoderException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    private void testConvert(String item, float size, float quote) throws EncoderException {
        String file = getFile("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.%s.mp4",
                targetFile, (int) (size * 100), (int) (quote * 100), item);
        MediaHelperExtend.zipVideo(file, name, "mp4", item, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }


    private String getFile(String file) {
        // 注意,路径应为文件在工程中的相对路径
        File f = new File("src/test/java/helper/" + file);
        if (!f.exists()) {
            System.out.println(f.getAbsoluteFile());
            throw new RuntimeException("视频文件不存在");
        }
        return f.getAbsolutePath();
    }


    private String getTargetFile(String file) {
        // 注意,路径应为文件在工程中的相对路径
        File f = new File(file);
        return f.getAbsolutePath();
    }


    @Test
    public void testImageZip() throws IOException {
        List<Runnable> list = new ArrayList<>();
        for (int i = 0; i < sizes.length; i++) {
            list.add(testImageZipThread(sizes[i], quotes[i]));
        }
        RunnableListAuto.run(list);
    }


    private Runnable testImageZipThread(float size, float quote) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    testImageZip(size, quote);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    private void testImageZip(float size, float quote) throws IOException {
        String file = getTargetFile("target/100M.mp4") + ".jpg";
        String targetFile = getTargetFile("target/100M.mp4");
        String toName = String.format("%s.size_%d.quot_%d.jpg",
                targetFile, (int) (size * 100), (int) (quote * 100));
        String toNameSystem = String.format("%s.size_%d.quot_%d.system.jpg",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.compressPic(file, toName, quote, size);
        MediaHelperExtend.compressPicBySystem(file, toNameSystem, quote, size);
    }


    @Test
    public void testVideoZipFlv() {
        List<Runnable> list = new ArrayList<>();
        for (int i = 0; i < sizes.length; i++) {
            list.add(testVideoZipFlvThread(sizes[i], quotes[i]));
        }
        RunnableListAuto.run(list);
    }


    private Runnable testVideoZipFlvThread(float size, float quote) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    testVideoZipFlv(size, quote);
                } catch (EncoderException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    private void testVideoZipFlv(float size, float quote) throws EncoderException {
        String file = getFile("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.flv",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideoFlv(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }


    @Test
    public void testVideoZipMpeg_50_50() throws EncoderException {
        testVideoZipMpeg(0.5f, 0.5f);
    }

    @Test
    public void testVideoZipMpeg() throws EncoderException {
        List<Runnable> list = new ArrayList<>();
        for (int i = 0; i < sizes.length; i++) {
            list.add(testVideoZipMpegThread(sizes[i], quotes[i]));
        }
        RunnableListAuto.run(list);
    }


    private Runnable testVideoZipMpegThread(float size, float quote) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    testVideoZipMpeg(size, quote);
                } catch (EncoderException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    private void testVideoZipMpeg(float size, float quote) throws EncoderException {
        String file = getFile("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.mpeg.mp4",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideoMpeg(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }

    @Test
    public void testVideoZip3pg() throws EncoderException {
        List<Runnable> list = new ArrayList<>();
        for (int i = 0; i < sizes.length; i++) {
            list.add(testVideoZip3pgThread(sizes[i], quotes[i]));
        }
        RunnableListAuto.run(list);
    }

    private Runnable testVideoZip3pgThread(float size, float quote) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    testVideoZip3pg(size, quote);
                } catch (EncoderException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    private void testVideoZip3pg(float size, float quote) throws EncoderException {
        String file = getFile("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.3pg",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideo3pg(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }


    @Test
    public void testVideoZip_50_50() throws EncoderException {
        testVideoZip(0.5f, 0.5f);
    }


    @Test
    public void testVideoZip() throws EncoderException {
        List<Runnable> list = new ArrayList<>();
        for (int i = 0; i < sizes.length; i++) {
            list.add(testVideoZipThread(sizes[i], quotes[i]));
        }
        RunnableListAuto.run(list);
    }

    private Runnable testVideoZipThread(float size, float quote) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    testVideoZip(size, quote);
                } catch (EncoderException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

    private void testVideoZip(float size, float quote) throws EncoderException {
        String file = getFile("100M.mp4");
        String targetFile = getTargetFile("target/100M.mp4");
        String name = String.format("%s.size_%d.quot_%d.mp4",
                targetFile, (int) (size * 100), (int) (quote * 100));
        MediaHelperExtend.zipVideo(file, name, size, (int) (MediaHelperExtend.DEFAULT_BIT_RATE * quote));
    }
}