package com.ylcm.sys.vo;

import com.ylcm.sys.domain.AdTheatreTv;
import com.ylcm.sys.enums.ProvinceEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReferenceArray;

@ApiModel("触达")
@Data
public class AdTvZonePeopleVO {

    @ApiModelProperty("省")
    private String province;

    @ApiModelProperty("触达/千人")
    private Integer num;

    public static List<AdTvZonePeopleVO> from(AdTheatreTv adTheatreTv) {
        if (adTheatreTv == null){
            adTheatreTv = new AdTheatreTv();
        }
        List<AdTvZonePeopleVO> vos = new ArrayList<>();


        for (ProvinceEnum e : ProvinceEnum.values()) {
            AdTvZonePeopleVO vo = new AdTvZonePeopleVO();
            vo.setProvince(e.getName());

            try {
                Integer id = (Integer) MethodUtils.invokeMethod(adTheatreTv, "get" + e.getCode().substring(0,1).toUpperCase() + e.getCode().substring(1, e.getCode().length()), null);
                vo.setNum(id);
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
            } catch (NoSuchMethodException ex) {
                ex.printStackTrace();
            }

            vos.add(vo);
        }

        vos.sort(new Comparator<AdTvZonePeopleVO>() {
            @Override
            public int compare(AdTvZonePeopleVO o1, AdTvZonePeopleVO o2) {
                if (o1.getNum() != null && o2.getNum() != null) {
                    return o2.getNum() - o1.getNum();
                }
                return 0;
            }
        });

        return vos.subList(0, 10);
    }
}
