package com.android.launcher3.model;


import com.android.launcher3.manager.ColumnDef;
import com.android.launcher3.util.Logger;
import com.android.launcher3.util.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TimeConfig {
    private int workMode;
    private int lockScreenMode;
    private List<AppTimeConfig> appTimeConfigs;
    private List<TimeRegion> regionList;
    //    休息时间描述
    private String restTimeRegion;
    //    使用时间描述
    private String useTimeRegion;
    //    游戏分钟数
    private long gameUseTimestamp = 0;

    private TimeConfig() {

    }


    public int getWorkMode() {
        return workMode;
    }

    public void setWorkMode(int workMode) {
        this.workMode = workMode;
    }

    public int getLockScreenMode() {
        return lockScreenMode;
    }

    public void setLockScreenMode(int lockScreenMode) {
        this.lockScreenMode = lockScreenMode;
    }

    public List<AppTimeConfig> getAppTimeConfigs() {
        return appTimeConfigs;
    }

    public void setAppTimeConfigs(List<AppTimeConfig> appTimeConfigs) {
        this.appTimeConfigs = appTimeConfigs;
    }

    public List<TimeRegion> getRegionList() {
        return regionList;
    }

    public void setRegionList(List<TimeRegion> regionList) {
        this.regionList = regionList;
    }

    public String getRestTimeRegion() {
        return restTimeRegion;
    }

    public void setRestTimeRegion(String restTimeRegion) {
        this.restTimeRegion = restTimeRegion;
    }

    public String getUseTimeRegion() {
        return useTimeRegion;
    }

    public void setUseTimeRegion(String useTimeRegion) {
        this.useTimeRegion = useTimeRegion;
    }

    public long getGameUseTimestamp() {
        return gameUseTimestamp;
    }

    public void setGameUseTimestamp(long gameUseTimestamp) {
        this.gameUseTimestamp = gameUseTimestamp;
    }

    public static class Builder {
        private int workMode;
        private int lockScreenMode = ColumnDef.LockScreen.MODE_REST;
        private Map<Integer, AppTimeConfig> appTimeConfigs;
        private List<TimeRegion> regionList;

        public Builder(@ColumnDef.TimeManager.TimeMode int workMode) {
            this.workMode = workMode;
            appTimeConfigs = new HashMap<>();
            regionList = new ArrayList<>();
        }


        /**
         * 工作模式
         *
         * @param workMode
         * @return
         */
        public Builder workMode(@ColumnDef.TimeManager.TimeMode int workMode) {
            this.workMode = workMode;
            return this;
        }


        /**
         * 该工作模式下不在UseRegion指定的时间范围内的锁屏模式
         * 默认为休息模式锁屏
         *
         * @param lockScreenMode
         * @return
         */
        public Builder lockScreenMode(@ColumnDef.LockScreen.LockScreenMode int lockScreenMode) {
            this.lockScreenMode = lockScreenMode;
            return this;
        }

        /**
         * 对对应app类型设置每天的最大使用时长
         *
         * @param appType
         * @param useTimeStamp
         * @return
         */
        public Builder addAppTypeConfig(@ColumnDef.AppUseType.UseTypeMode int appType, long useTimeStamp) {
            AppTimeConfig timeConfig = new AppTimeConfig(appType, useTimeStamp);

            appTimeConfigs.put(appType, timeConfig);
            return this;
        }


        /**
         * 添加对应模式的应用可使用的时间段 格式为[开始时间,结束时间,开始时间,结束时间,开始时间,结束时间,...]
         *
         * @param userRegion
         * @return
         */
        public Builder addUseRegion(List<Long> userRegion) {
            if (userRegion == null)
                return this;
            if (userRegion.isEmpty())
                return this;

            if (userRegion.size() % 2 != 0) {
                throw new IllegalArgumentException("userRegion size must be Even number");
            }

            for (int i = 0; i < userRegion.size(); i += 2) {
                TimeRegion timeRegion = new TimeRegion();

                timeRegion.setStartTime(userRegion.get(i));
                timeRegion.setEndTime(userRegion.get(i + 1));
                regionList.add(timeRegion);
            }

            return this;
        }


        public TimeConfig build() {

            TimeConfig timeConfig = new TimeConfig();

            timeConfig.setWorkMode(workMode);

            timeConfig.setLockScreenMode(lockScreenMode);

            List<AppTimeConfig> appConfigs = new ArrayList<>();

            if (!appTimeConfigs.isEmpty()) {

                Iterator<AppTimeConfig> iterator = appTimeConfigs.values().iterator();

                while (iterator.hasNext()) {
                    appConfigs.add(iterator.next());
                }

                AppTimeConfig appTimeConfig = appTimeConfigs.get(ColumnDef.AppUseType.GAME_LOCK_TYPE);

                if (appTimeConfig != null) {
                    timeConfig.setGameUseTimestamp(appTimeConfig.appUseTimeSecond);
                }
            }


            timeConfig.setAppTimeConfigs(appConfigs);


            timeConfig.setRegionList(regionList);


            if (regionList == null || regionList.isEmpty()) {
                timeConfig.setRestTimeRegion("00:00-24:00");
                timeConfig.setUseTimeRegion("无");
            } else {

                long dayStart = 0;

                long dayEnd = 24 * 3600;
                long dayStartTime = TimeUtils.getTimeZeroClock();

                StringBuilder restTime = new StringBuilder();

                StringBuilder useTime = new StringBuilder();
//                区分单双 添加\n  \t等转义字符
                boolean useSingle = false;
                boolean restSingle = false;

                for (TimeRegion region : regionList) {

                    if (region.getStartTime() < 0) {
                        region.setStartTime(0);
                    }

                    if (region.getStartTime() > dayStart) {
                        restTime.append(TimeUtils.getTime(dayStartTime + dayStart*1000, TimeUtils.DATE_FORMAT_HH_MM)).append("-");
                        restTime.append(TimeUtils.getTime(dayStartTime + region.getStartTime() * 1000, TimeUtils.DATE_FORMAT_HH_MM));

                        restSingle = !restSingle;
                        if (restTime.length() > 0 && !restSingle) {
                            restTime.append("\n");
                        } else {
                            restTime.append("\t");
                        }


                    }

                    useTime.append(TimeUtils.getTime(dayStartTime + region.getStartTime() * 1000, TimeUtils.DATE_FORMAT_HH_MM)).append("-");
                    useTime.append(TimeUtils.getTime(dayStartTime + region.getEndTime() * 1000, TimeUtils.DATE_FORMAT_HH_MM));

                    useSingle = !useSingle;

                    if (useTime.length() > 0 && !useSingle) {
                        useTime.append("\n");
                    } else {
                        useTime.append("\t");
                    }

                    if (region.getEndTime() > dayEnd) {
                        region.setEndTime(dayEnd);
                    }


                    dayStart = region.getEndTime();

                }


                TimeRegion endRegion = regionList.get(regionList.size() - 1);

                if (endRegion.getEndTime() < dayEnd) {
                    restTime.append(TimeUtils.getTime(dayStartTime + endRegion.getEndTime() * 1000, TimeUtils.DATE_FORMAT_HH_MM)).append("-");
                    restTime.append(TimeUtils.getTime(dayStartTime + dayEnd*1000, TimeUtils.DATE_FORMAT_HH_MM));

                    if (restTime.length() > 0 && restSingle) {
                        restTime.append("\n");
                    } else {
                        restTime.append("\t");
                    }
                }

                timeConfig.setUseTimeRegion(useTime.toString());
                timeConfig.setRestTimeRegion(restTime.toString());
            }


            Logger.e("TimeConfig", timeConfig.getRestTimeRegion() + timeConfig.getUseTimeRegion());
            return timeConfig;
        }

    }


}
