package com.crm.rely.backend.model.util;


import com.crm.rely.backend.model.pojo.table.CustomInfoTable;
import com.crm.rely.backend.model.pojo.view.CustomView;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.google.common.base.Strings;

public class CustomInfoUtil {

    /**
     * ase 正序( firstName middle lastName) desc 倒序 正序(lastName middle firstName)
     */
    private static String DEFAULT_ASE = "ASE";

    public static final String ORDER_ASE = "ASE";

    public static final String ORDER_DESC = "DESC";

    /**
     * 获取客户姓名
     *
     * @param table
     * @return
     */
    public static String getName(CustomInfoTable table) {

        return getName(table, null);
    }

    /**
     * @param table
     * @param order 顺序 ace 正序( firstName middle lastName) desc 倒序 正序(lastName middle firstName)
     * @return
     */
    public static String getName(CustomInfoTable table, String order) {
        if (table == null) {
            return null;
        }
        return getName(table.getFirstName(), table.getMiddle(), table.getLastName(), null, order);
    }

    public static String getName(CustomInfoEntity entity) {
        return getName(entity, null);
    }

    public static String getName(CustomInfoEntity entity, String order) {
        if (entity == null) {
            return null;
        }
        return getName(entity.getFirstName(), entity.getMiddle(), entity.getLastName(), null, order);
    }

    public static String getName(CustomView view) {
        return getName(view, null);
    }

    public static String getName(CustomView view, String order) {
        if (view == null) {
            return null;
        }
        return getName(view.getFirstName(), view.getMiddle(), view.getLastName(), null, order);
    }


    private static String getName(String firstName, String middle, String lastName) {
        return getName(firstName, middle, lastName, " ");
    }

    public static String getName(String firstName, String middle, String lastName, String fill) {
        return getName(firstName, middle, lastName, fill, null);
    }

    public static String getName(String firstName, String middle, String lastName, String fill, String order) {
        if (fill == null) {
            fill = " ";
        }
        if (Strings.isNullOrEmpty(order)) {
            order = DEFAULT_ASE;
        }
        StringBuffer nameSbuff = new StringBuffer();

        if (ORDER_ASE.equals(order)) {
            append(nameSbuff, fill, firstName);
        } else {
            append(nameSbuff, fill, lastName);
        }

        append(nameSbuff, fill, middle);

        if (ORDER_ASE.equals(order)) {
            append(nameSbuff, fill, lastName);
        } else {
            append(nameSbuff, fill, firstName);
        }

        return nameSbuff.toString();
    }

    private static void append(StringBuffer nameSbuff, String fill, String str) {
        if (nameSbuff == null) {
            nameSbuff = new StringBuffer(20);
        }
        if (!Strings.isNullOrEmpty(str)) {
            if (nameSbuff.length() > 0) {
                if (!Strings.isNullOrEmpty(fill)) {
                    nameSbuff.append(fill);
                }
            }
            nameSbuff.append(str);
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getName("123", "234", "456", "-", null));
    }

}
