package com.wisewoods.eduandroid.app;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.wisewoods.base.app.BaseApp;
import com.wisewoods.base.app.UserManager;
import com.wisewoods.base.bean.BaseList;
import com.wisewoods.base.bean.IMToken;
import com.wisewoods.base.bean.MsgCount;
import com.wisewoods.base.bean.Opt;
import com.wisewoods.base.bean.Role;
import com.wisewoods.base.net.ApiService;
import com.wisewoods.base.net.JCallback;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.utils.$;
import com.wisewoods.base.utils.DialogUtils;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.ui.BuildingBaseActivity;
import com.wisewoods.eduandroid.ui.ChangePwdActivity;
import com.wisewoods.eduandroid.ui.LiveListActivity;
import com.wisewoods.eduandroid.ui.MessageActivity;
import com.wisewoods.eduandroid.ui.MettingActivity;
import com.wisewoods.eduandroid.ui.ProjectActivity;
import com.wisewoods.eduandroid.ui.RoomActivity;
import com.wisewoods.eduandroid.ui.RoomLiveActivity;
import com.wisewoods.eduandroid.ui.TaskActivity;
import com.wisewoods.eduandroid.ui.TeacherCourseActivity;
import com.wisewoods.eduandroid.ui.TeacherInfoActivity;
import com.wisewoods.eduandroid.ui.TeacherScheduleActivity;
import com.wisewoods.eduandroid.ui.VersionActivity;
import com.wisewoods.eduandroid.ui.WebActivity;
import com.wisewoods.eduandroid.ui.adapter.MainTab;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;

import retrofit2.Response;

public class ImgManager {

    public static HashMap<String, MainTab> maps = new HashMap<>();
    public static HashMap<String, String> sexMaps = new HashMap<>();
    public static HashMap<Integer, String> trainingTypeMaps = new HashMap<>();
    public static HashMap<Integer, String> auditStatusMaps = new HashMap<>();
    public static HashMap<Integer, Integer> auditColorMaps = new HashMap<>();

//    public static final String COS_PATH_COURSE_FILE = "";
//    public static final String COS_PATH_COURSE_IMG = "";
//    public static final String COS_PATH_RES_FILE = "";
//    public static final String COS_PATH_BASE_IMG = "";
//    public static final String COS_PATH_BASE_VIDEO = "";
//    public static final String COS_PATH_USER_FACE = "";
//    public static final String COS_PATH_TEACHER_FACE = "";
//    public static final String COS_PATH_TEACHER_CER = "";
//    public static final String COS_PATH_TEACHER_IDCARD = "";
//    public static final String COS_PATH_TEACHER_BANKCARD = "";
//    public static final String COS_PATH_STU_LEAVE = "";
//    public static final String COS_PATH_TASK_IMG = "";

    public static String getSex(String key) {
        String text = "";
        try {
            text = sexMaps.get(key);
        } catch (Exception e) {

        }
        return text;
    }

    public static String getTrainingType(Integer key) {
        String text = "";
        try {
            text = trainingTypeMaps.get(key);
        } catch (Exception e) {

        }
        return text;
    }

    public static String getAudit(Integer key) {
        String text = "";
        try {
            text = auditStatusMaps.get(key);
        } catch (Exception e) {

        }
        return text;
    }

    public static Integer getAuditColor(Integer key) {
        int color = 0;
        try {
            color = auditColorMaps.get(key);
        } catch (Exception e) {

        }
        return color;
    }

    public static ArrayList<Opt> getSexList() {
        ArrayList<Opt> al = new ArrayList<>();
        for (String s : sexMaps.keySet()) {
            al.add(new Opt(s, sexMaps.get(s)));
        }
        return al;
    }

    static {
        sexMaps.put("0", "男");
        sexMaps.put("1", "女");
        sexMaps.put("2", "其他");

        trainingTypeMaps.put(1, "线上");
        trainingTypeMaps.put(2, "线下");
        trainingTypeMaps.put(3, "线上线下结合");

        auditStatusMaps.put(0, "待审核");
        auditStatusMaps.put(1, "审核完成");
        auditStatusMaps.put(2, "审核未通过");

        auditColorMaps.put(0, Color.parseColor("#fffe9b00"));
        auditColorMaps.put(1, Color.parseColor("#ff347fff"));
        auditColorMaps.put(2, Color.parseColor("#ffff4b4b"));

        maps.put("培训信息", new MainTab("培训信息", R.drawable.ic_tab_project, ProjectActivity.class));
        maps.put("待办任务", new MainTab("待办任务", R.drawable.ic_tab_task, v -> {
            TaskActivity.start("待办任务", 0);
        }));
        maps.put("完成任务", new MainTab("完成任务", R.drawable.ic_tab_complete, v -> {
            TaskActivity.start("完成任务", 1);
        }));
        maps.put("会议", new MainTab("会议", R.drawable.ic_tab_metting, MettingActivity.class));
        maps.put("直播列表", new MainTab("直播列表", R.drawable.ic_tab_live, v -> {

            BaseApp.api.genUserSig(UserManager.getWorkid()).enqueue(new JDCallback<IMToken>() {
                @Override
                public void onS(Response<IMToken> response, IMToken imToken) {
                    BaseApp.SDKAPPID = imToken.data.appID;
                    com.tencent.liteav.live.model.TRTCMeeting.sharedInstance(Utils.getApp())
                            .login(imToken.data.appID, UserManager.getWorkid(), imToken.data.userSig,
                                    (code, msg) -> {
                                        LogUtils.d("code: " + code + " msg:" + msg);
                                        if (code == 0) {
                                            ActivityUtils.startActivity(LiveListActivity.class);
                                        } else {
                                            $.toast(msg);
                                        }
                                    });
                }

            });
        }));
        maps.put("教室统计", new MainTab("教室统计", R.drawable.ic_tab_room, RoomActivity.class));
//        maps.put("宿舍查询", new MainTab("宿舍查询", R.drawable.ic_tab_sleep, HostelActivity.class));
        maps.put("基地管理", new MainTab("基地管理", R.drawable.ic_tab_base, v -> {

            App.api.selectBaseLibraryListByStuId()
                    .enqueue(new JCallback<BaseList>() {
                        @Override
                        public void onS(Response<BaseList> response, BaseList baseList) {
                            ArrayList<CharSequence> items = new ArrayList<>();
                            for (BaseList.ListBean item : baseList.data) {
                                items.add(item.base_name + "");
                            }
                            if (items.size() > 1) {
                                DialogUtils.showList("请选择基地", items, (materialDialog, index, text) -> {
                                    BuildingBaseActivity.start(baseList.data.get(index).id + "");
                                    return null;
                                });
                            } else if (items.size() == 1) {
                                BuildingBaseActivity.start(baseList.data.get(0).id + "");
                            } else {
                                $.toast("无基地信息");
                            }
                        }
                    });
        }));
        maps.put("当前课程", new MainTab("当前课程", R.drawable.ic_teacher_course, v -> {
            TeacherScheduleActivity.start("当前课程", 0);
        }));
        maps.put("历史课程", new MainTab("历史课程", R.drawable.ic_history_course, v -> {
            TeacherScheduleActivity.start("历史课程", 1);
        }));
        maps.put("师资信息", new MainTab("师资信息", R.drawable.ic_teacher_info,
                v -> {
                    App.api.queryTeacherInfoCount(UserManager.getUid()).enqueue(new JDCallback<MsgCount>() {
                        @Override
                        public void onS(Response<MsgCount> response, MsgCount msgCount) {
                            if (msgCount.data.auditCount > 0) {
                                $.toast("师资信息正在审核中...");
                            } else {
                                ActivityUtils.startActivity(TeacherInfoActivity.class);
                            }
                        }
                    });

                }));
        maps.put("课程管理", new MainTab("课程管理", R.drawable.ic_course_manage, TeacherCourseActivity.class));

        maps.put("通知", new MainTab("通知", R.drawable.ic_tab_notice, MessageActivity.class));
        maps.put("使用指南", new MainTab("使用指南", R.drawable.ic_tab_help, v -> {
            WebActivity.start(ApiService.HELP_ADDRESS, "使用指南");
        }));
        maps.put("修改密码", new MainTab("修改密码", R.drawable.ic_tab_setting, ChangePwdActivity.class));
        maps.put("版本信息", new MainTab("版本信息", R.drawable.ic_tab_version, VersionActivity.class));
        maps.put("退出", new MainTab("退出", R.drawable.ic_tab_logout, v -> {
            DialogUtils.showCommon(
                    "确认退出当前账号？", (DialogUtils.IDialogOnOKListener) dialog -> {
                            UserManager.logout();
                    });

        }));
        maps.put("教室互动", new MainTab("教室互动", R.drawable.ic_class_room, v -> {

            BaseApp.api.genUserSig(UserManager.getInterlocutorsUid()).enqueue(new JCallback<IMToken>() {
                @Override
                public void onS(Response<IMToken> response, IMToken imToken) {
                    BaseApp.SDKAPPID = imToken.data.appID;
                    RoomLiveActivity.start(imToken.data.appID, imToken.data.userSig);

//                    TRTCMeeting.sharedInstance(Utils.getApp())
//                            .login(imToken.data.appID, UserManager.getInterlocutorsUid(), imToken.data.userSig,
//                                    (code, msg) -> {
//                                        LogUtils.d("code: " + code + " msg:" + msg);
//                                        if (code == 0) {
//
//                                            Intent intent = new Intent(ActivityUtils.getTopActivity(), CreateMeetingActivity.class);
//                                            intent.putExtra("TITLE", "教室互动");
//                                            ActivityUtils.startActivity(intent);
//
//                                        } else {
//                                            $.toast(msg);
//                                        }
//                                    });

                }

            });


        }));
    }


    public static ArrayList<MainTab> getMenu() {
        ArrayList<MainTab> mainTabs = new ArrayList<>();
        LinkedHashSet<String> set = new LinkedHashSet<>();

        Role role = UserManager.getUser().getRole();
        switch (role) {
            case TEACHER:
                set.add("当前课程");
                set.add("历史课程");
                set.add("师资信息");
                set.add("课程管理");
                break;
            case BASE:
                set.add("基地管理");
                break;
            case ORGANIZER:
            case UNDERTAKER:
                set.add("培训信息");
                set.add("直播列表");
                break;
            case EDUCATION:
                set.add("培训信息");
                set.add("教室统计");
                set.add("待办任务");
                set.add("完成任务");
                set.add("会议");
                set.add("直播列表");
                break;
            case TRAIN:
                set.add("培训信息");
                set.add("待办任务");
                set.add("完成任务");
                set.add("会议");
                set.add("直播列表");
                break;
            case LOGISTICS:
//                set.add("宿舍查询");
                set.add("待办任务");
                set.add("完成任务");
                set.add("会议");
                set.add("直播列表");
                break;
            default:
                set.add("待办任务");
                set.add("完成任务");
                set.add("直播列表");
                break;
        }

        set.add("教室互动");
        set.add("通知");
        set.add("使用指南");
        set.add("修改密码");
        set.add("版本信息");
        set.add("退出");
        for (String s : set) {
            mainTabs.add(maps.get(s));
        }
        return mainTabs;
    }

    public static void selectFile(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");//设置类型，我这里是任意类型，可以过滤文件类型
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        activity.startActivityForResult(intent, 8000);
    }

    /**
     * 校验银行卡卡号
     *
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            //如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

}
