package com.crm.rely.backend.util;

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

public class CustomInfoUtil {


    /**
     * ase 正序( firstName middle lastName) desc 倒序 正序(lastName middle firstName)
     */
    public static final String ORDER_ASE = "ASE";

    public static final String ORDER_DESC = "DESC";

    /**
     * 获取客户姓名
     *
     * @param table
     * @return
     */
    public static String getName(CustomInfoTable table) {
        if (CountryUtil.isCTHM(table.getCountry())) {
            if ((!Strings.isNullOrEmpty(table.getFirstName()) && table.getFirstName().contains(".")) || (!Strings.isNullOrEmpty(table.getLastName()) && table.getLastName().contains(".")) || ("" + table.getFirstName() + table.getLastName()).length() >= 5) {
                return getName(table, ORDER_DESC);
            } else {
                return getName(table, ORDER_DESC);
            }
        } else {
            return getName(table, ORDER_ASE);
        }

    }

    /**
     * @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(CustomAccountExportView view) {
        if (CountryUtil.isCTHM(view.getCountry())) {
            return getName(view, ORDER_DESC);
        } else {
            return getName(view, ORDER_ASE);
        }
    }

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

    public static String getName(CustomInfoEntity entity) {
        if (CountryUtil.isCTHM(entity.getCountry())) {
            return getName(entity, ORDER_DESC);
        } else {
            return getName(entity, ORDER_ASE);
        }
    }

    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) {
        if (CountryUtil.isCTHM(view.getCountry())) {
            return getName(view, ORDER_DESC);
        } else {
            return getName(view, ORDER_ASE);
        }
    }

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

    public static String getName(CustomInfoGetEmailView view) {
        if (CountryUtil.isCTHM(view.getCountry())) {
            return getName(view, ORDER_DESC);
        } else {
            return getName(view, ORDER_ASE);
        }

    }

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

    public static String getName(CustomInfoGetCidView view) {
        if (CountryUtil.isCTHM(view.getCountry())) {
            return getName(view, ORDER_DESC);
        } else {
            return getName(view, ORDER_ASE);
        }
    }

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

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

    public static String getNameByCountry(String firstName, String middle, String lastName, String country) {
        if (CountryUtil.isCTHM(country)) {
            return getName(firstName, middle, lastName, null, ORDER_DESC);
        } else {
            return getName(firstName, middle, lastName, null, ORDER_ASE);

        }
    }

    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 (Strings.isNullOrEmpty(order)) {
            order = ORDER_ASE;
        }

        if (fill == null) {
            if (ORDER_ASE.equals(order)) {
                fill = " ";
            } else {
                fill = "";
            }
        }

        StringBuffer nameSbuff = new StringBuffer();

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

        if (!Strings.isNullOrEmpty(middle)) {
            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 Integer isXJ(CustomInfoTable table) {
        String name = getName(table);
        return isXJ(table.getCountry(), name);
    }

    public static Integer isXJ(CustomInfoEntity entity) {
        String name = getName(entity);
        return isXJ(entity.getCountry(), name);
    }

    private static Integer isXJ(String country, String name) {

        Integer xj = 0;
        if ("CN".equals(country)) {
            if (!Strings.isNullOrEmpty(name) && name.trim().length() > 4 && (name.indexOf(".") > 0 || name.indexOf(
                    "·") > 0)) {
                xj = 1;
            }
        }
        return xj;
    }

    public static void main(String[] args) throws Exception {
        System.out.println("苏·托乎提".length());
    }

}
