package com.sheep.gamegroup.util;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import rx.functions.Action1;

/**
 * Created by realicing on 2018/5/3.
 * realicing@sina.com
 */
public class ListUtil {
    public static <T> boolean hasIndex(List<T> list, int index) {
        return !ListUtil.isEmpty(list) && list.size() > index && index > -1;
    }

    public static <T> boolean isEmpty(List<T> list) {
        return list == null || list.isEmpty();
    }

    public static <T> void forEach(List<T> list, Action1<T> action1) {
        if(list != null)
            for (T t : list) {
                action1.call(t);
            }
    }

    public static <T> T getItem(List<T> list, int index) {
        return hasIndex(list, index) ? list.get(index) : null;
    }

    public static <T> T getLast(List<T> list) {
        int size = ListUtil.size(list);
        return size > 0 ? list.get(-1 + size) : null;
    }


    public static int getTotalPage(int count, int per_page) {
        return (count - 1) / per_page + 1;
    }

    public static <T> ArrayList<T> emptyList() {
        return new ArrayList<>();
    }

    public static <R, V> int addAll(Conversion<R, V> conversion, List<V> list, R... elements) {
        int length = elements.length;
        for (int i = 0; i < length; i++) {
            V v = conversion.convert(elements[i], i);
            list.add(v);
        }
        return length;
    }

    public static <R, V> int addAll(Conversion<R, V> conversion, List<V> list, List<R> elements) {
        int size = elements.size();
        for (int i = 0; i < elements.size(); i++) {
            V v = conversion.convert(elements.get(i), i);
            list.add(v);
        }
        return size;
    }

    public static <T> int addAll(List<T> list, T... elements) {
        return addAll(list, asList(elements));
    }

    public static <T> int addAll(List<T> list, List<? extends T> elements) {
        int count = 0;
        if (list == null) {
            list = emptyList();
        }
        if (elements != null) {
            for (T element : elements) {
                int index = list.indexOf(element);
                if (index != -1) {
                    list.set(index, element);
                } else if (list.add(element)) {
                    count++;
                }
            }
        }
        return count;
    }

    public static <T> int addAll(List<T> list, List<? extends T> elements, int maxCount) {
        int count = 0;
        if (list == null) {
            list = emptyList();
        }
        if (elements != null) {
            for (T element : elements) {
                int index = list.indexOf(element);
                if (index != -1) {
                    list.set(index, element);
                } else if (list.add(element)) {
                    count++;
                }
                if (count >= maxCount)//最多只加maxCount条数据
                    break;
            }
        }
        return count;
    }

    public static <T> int addAllItem(List<T> list, List<? extends T> elements) {
        int count = 0;
        if (list == null) {
            list = emptyList();
        }
        if (elements != null) {
            for (T element : elements) {
                list.add(element);
                count++;
            }
        }
        return count;
    }

    public static <T> int addAllItem(List<T> list, List<? extends T> elements, int maxCount) {
        int count = 0;
        if (list == null) {
            list = emptyList();
        }
        if (elements != null) {
            for (T element : elements) {
                list.add(element);
                count++;
                if (count >= maxCount)//最多只加maxCount条数据
                    break;
            }
        }
        return count;
    }

    public static <T> boolean isLastPosition(List<T> list, int position) {
        return size(list) == position + 1;
    }

    public static <T> int size(List<T> list) {
        return isEmpty(list) ? 0 : list.size();
    }

    @SafeVarargs
    public static <T> ArrayList<T> asList(T... items) {
        ArrayList<T> list = emptyList();
        Collections.addAll(list, items);
        return list;
    }
    public static <T> ArrayList<T> asList(List<T> itemList) {
        if(itemList instanceof ArrayList){
            return (ArrayList<T>) itemList;
        }
        ArrayList<T> list = emptyList();
        addAllItem(list, itemList);
        return list;
    }

    @SafeVarargs
    public static <T, R> ArrayList<R> asList(CallBack<T, R> callBack, T... items) {
        ArrayList<R> list = emptyList();
        for (T t : items) {
            R r = callBack.call(t);
            list.add(r);
        }
        return list;
    }
    public static <T, R> ArrayList<R> asList(CallBack<T, R> callBack, List<T> itemList, CallBack<T, Boolean> filterCallBack) {
        ArrayList<R> list = emptyList();
        for (T t : itemList) {
            R r = callBack.call(t);
            if(filterCallBack == null || filterCallBack.call(t))
                list.add(r);
        }
        return list;
    }

    public static <T> ArrayList<T> removeItem(List<T> list, CallBack<T, Boolean> callBack) {
        if (isEmpty(list)) {
            return null;
        }
        ArrayList<T> removeList = emptyList();
        for (T t : list) {
            if (callBack.call(t))
                removeList.add(t);
        }
        list.removeAll(removeList);
        return removeList;
    }
    public static <T> T getItem(List<T> list, CallBack<T, Boolean> callBack) {
        if (isEmpty(list)) {
            return null;
        }
        for (T t : list) {
            if (callBack.call(t))
                return t;
        }
        return null;
    }

    public static <T> void set(List<T> list, T t, int position) {
        if (hasIndex(list, position))
            list.set(position, t);
    }

    /**
     * 清除末尾的空数据
     *
     * @param list
     */
    public static <T> void removeNull(List<T> list) {
        removeItem(list, new CallBack<T, Boolean>() {
            @Override
            public Boolean call(T t) {
                return t == null || t.toString().isEmpty();
            }
        });
    }

    public static <T> void removeAll(List<T> allList, List<T> removeList) {
        if (allList != null && removeList != null)
            allList.removeAll(removeList);
    }
    //I 传入的数据， R 返回的结果
    public static interface CallBack<I, R> {
        public R call(I i);
    }
    //列表拼接为字符串
    public static String listToUrls(List<String> list){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if(i != 0)
                sb.append(";");
            sb.append(list.get(i));
        }
        return sb.toString();
    }
}
