package com.android.launcher3.debug.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.manager.ColumnDef;
import com.android.launcher3.manager.TimeModeManager;
import com.android.launcher3.model.AppTimeConfig;
import com.android.launcher3.model.TimeConfig;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class WorkModeAdapter extends RecyclerView.Adapter<WorkModeAdapter.ViewHolder> {

    private List<TimeConfig> timeConfigs;

    private LayoutInflater mInflater;

    public void setTimeConfigs(List<TimeConfig> timeConfigs) {
        this.timeConfigs = timeConfigs;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(parent.getContext());
        }
        return new ViewHolder(mInflater.inflate(R.layout.adapter_work_mode_item, null));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bindData(timeConfigs.get(position));
    }

    @Override
    public int getItemCount() {
        return timeConfigs == null ? 0 : timeConfigs.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_mode_name;
        private TextView tv_mode_use_time;
        private TextView tv_mode_rest_time;
        private TextView tv_mode_game;
        private TextView tv_app_type_limit_time;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_mode_name=itemView.findViewById(R.id.tv_mode_name);
            tv_mode_use_time=itemView.findViewById(R.id.tv_mode_use_time);
            tv_mode_rest_time=itemView.findViewById(R.id.tv_mode_rest_time);
            tv_mode_game=itemView.findViewById(R.id.tv_mode_game);
            tv_app_type_limit_time=itemView.findViewById(R.id.tv_app_type_limit_time);
        }


        public void bindData(TimeConfig config) {

            switch (config.getWorkMode()) {
                case ColumnDef.TimeManager.HOLIDAY_MODE:
                    tv_mode_name.setText("假期模式");
                    break;
                case ColumnDef.TimeManager.WEEK_RESET:
                    tv_mode_name.setText("周末模式");
                    break;
                case ColumnDef.TimeManager.WORK_DAY:
                    tv_mode_name.setText("工作日模式");
                    break;
                default:
                    tv_mode_name.setText("未设置模式");
            }
            tv_mode_use_time.setText(config.getRegionList().size()+"\t可使用时间段"+config.getUseTimeRegion());
            tv_mode_rest_time.setText(config.getRegionList().size()+"\t休息时间段"+config.getRestTimeRegion());
            tv_mode_game.setText("游戏使用时间："+config.getGameUseTimestamp()+"秒");

            if (config.getAppTimeConfigs()!=null){
                StringBuilder stringBuilder=new StringBuilder();
                for (AppTimeConfig appTimeConfig:config.getAppTimeConfigs()){
                    switch (appTimeConfig.appType){
                        case ColumnDef.AppUseType.GAME_LOCK_TYPE:
                            stringBuilder.append("游戏类使用时长"+appTimeConfig.appUseTimeSecond+"秒\n");
                            break;
                        case ColumnDef.AppUseType.LEARN_TYPE:
                            stringBuilder.append("学习类使用时长"+appTimeConfig.appUseTimeSecond+"秒\n");
                            break;
                        case ColumnDef.AppUseType.SAFE_TYPE:
                            stringBuilder.append("无需管控类使用时长"+appTimeConfig.appUseTimeSecond+"秒\n");
                            break;
                        case ColumnDef.AppUseType.UNCLASS_TYPE:
                            stringBuilder.append("未分类使用时长"+appTimeConfig.appUseTimeSecond+"秒\n");
                            break;
                    }
                }
                tv_app_type_limit_time.setText(stringBuilder.toString());

            }else {
                tv_app_type_limit_time.setText("无");
            }


        }
    }
}
