package com.phlx.anchorcollect_p.util;


import com.phlx.anchorcollect_p.Configs;
import com.phlx.anchorcollect_p.entity.DictEntity;
import com.phlx.anchorcollect_p.entity.FrozenSemenEntity;
import com.phlx.anchorcollect_p.entity.GenTableColumn;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import me.goldze.mvvmhabit.utils.StringUtils;

public class LoginInfoUtils {

    public static List<String> getDistNameList(String type){

        List<String> distNames = new ArrayList<>();
        for (DictEntity de: Configs.dictList) {
            if(type.equals(de.getDictType())){
                distNames.add(de.getDictLabel());
            }
        }

        return distNames;
    }

    public static List<DictEntity> getDistList(String type){

        List<DictEntity> dists = new ArrayList<>();
        for (DictEntity de:Configs.dictList) {
            if(type.equals(de.getDictType())){
                dists.add(de);
            }
        }

        return dists;
    }


    public static FrozenSemenEntity getFrozenSemenForName(String code) {
        for (FrozenSemenEntity frozenSemenEntity : Configs.frozenSemenList) {
            String[] infos = frozenSemenEntity.getInfo().split("/");
            if (infos != null && infos.length > 0) {
                for (int i = 0; i < infos.length; i++) {
                    if (infos[i].equals(code)) {
                        return frozenSemenEntity;
                    }
                }
            }
        }
        return null;
    }

    /**
     * 给单选设置默认值，给日期设置默认当天
     * @param gtc
     * @param dateFormat
     */
    public static void setGenTableColumnDefault(GenTableColumn gtc, SimpleDateFormat dateFormat){
        //增加单选默认值
        if (StringUtils.isEmpty(gtc.getContent()) && (gtc.getHtmlType().equals("select") || gtc.getHtmlType().equals("radio"))) {
            List<DictEntity> dictEntities = LoginInfoUtils.getDistList(gtc.getDictType());
            for (int i = 0; i < dictEntities.size(); i++) {
                if ("Y".equals(dictEntities.get(i).getIsDefault())) {
                    gtc.setContent(dictEntities.get(i).getDictValue());
                    gtc.setShowName(dictEntities.get(i).getDictLabel());
                }
            }
        }
        //增加时间默认值
        if (StringUtils.isEmpty(gtc.getContent()) && gtc.getHtmlType().equals("datetime")) {
            gtc.setContent(dateFormat.format(new Date()));
            gtc.setShowName(dateFormat.format(new Date()));
        }
    }

}
