import { HttpHeaders } from '@angular/common/http';
import { CourseData } from './structures/course-data';
import { Pages, PageInfo } from './structures/page-info';

export const isMock = (window.location.port == '4200');
export const target = (window.location.hostname == 'localhost' ? 'https://' + window.location.hostname + '/isod/www/' : 'http://' + window.location.hostname + '/');

export const snackbarSuccessTime = 2000;
export const snackbarErrorTime = 6000;
export const snackbarInfoTime = 6000;
export const snackbarInfiniteTime = 0;

export const instructorRoles = [ 'blokovany', 'cekatel', 'instruktor', 'zamestnanec', 'admin' ];
export const instructorRolesPretty = [ 'Blokovaný profil', 'Čekající na schválení', 'Instruktor', 'Zaměstnanec', 'Administrátor' ];

export const instructorCourseRoles = [ 'Vše', 'Vedoucí', 'Instruktor', 'Nováček' ];
export const defaultRates = [0, 1800, 1200, 500];

export const themeStates = [ 'aktivni', 'neaktivni' ];
export const themeStatesPretty = [ 'Aktivní', 'Neaktivní' ];

export const genders = [ 'muz', 'zena' ];
export const gendersPretty = [ 'Muž', 'Žena' ];

export const savingStatus = {
    none: 0,
    inProgress: 1,
    success: 2,
    failed: -1
}

export const dayNames = [ 'Neděle', 'Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota' ];
export const monthNames = [ 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Řijen', 'Listopad', 'Prosinec' ];

export const httpOptions = {
    headers: new HttpHeaders({
        'Content-Type':  'application/json'
    })
};

export const pages: Pages = {
    main: { text: 'Hlavní panel', icon: 'home', outlined: true, path: 'hlavni-panel' },
    mainNotApproved: { text: 'Úvodní informace', icon: 'home', outlined: true, path: 'uvodni-informace'},
    mainBlocked: { text: 'Profil blokován', icon: 'home', outlined: true, path: 'profil-blokovan'},
    myProfile: { text: 'Můj profil', icon: 'portrait', outlined: false, path: 'profil'},
    editMyProfile: { text: 'Upravit profil', icon: 'edit', outlined: false, path: 'upravit-profil'},
    courses: { text: 'Nabídka kurzů', icon: 'date_range', outlined: false, path: 'kurzy'},
    myCourses: { text: 'Moje kurzy', icon: 'event', outlined: false, path: 'moje-kurzy'},
    courseDetail: { text: 'Detail kurzu', icon: 'event', outlined: false, path: 'detail-kurzu'},
    newCourse: { text: 'Nový kurz', icon: 'add', outlined: false, path: 'novy-kurz'},
    courseEdit: { text: 'Upravit kurz', icon: 'edit', outlined: false, path: 'upravit-kurz'},
    instructorLogin: { text: 'Přihlášení instruktora', icon: 'login', outlined: false, path: 'prihlaseni-instruktora'},
    instructorRegister: { text: 'Vytvoření účtu instruktora', icon: 'person', outlined: false, path: 'registrace-instruktora'},
    instructors: { text: 'Instruktoři', icon: 'groups', outlined: true, path: 'instruktori'},
    instructorProfile: { text: 'text', icon: 'stop', outlined: false, path: 'profil-instruktora'},
    editInstructorProfile: { text: 'text', icon: 'stop', outlined: false, path: 'upravit-instruktora'},
    notifications: { text: 'Upozornění', icon: 'notifications', outlined: true, path: 'notifikace'},
    schoolLogin: { text: 'Přihlášení školy', icon: 'login', outlined: false, path: 'prihlaseni-skoly'},
    schoolRegister: { text: 'Vytvoření účtu školy', icon: 'school', outlined: false, path: 'registrace-skoly'},
    allThemes: { text: 'Témata kurzů', icon: 'style', outlined: true, path: 'temata-kurzu'},
    newTheme: { text: 'Nové téma', icon: 'add', outlined: true, path: 'nove-tema'},
    editTheme: { text: 'Upravit téma', icon: 'edit', outlined: true, path: 'upravit-tema'},
    allSchools: { text: 'Školy', icon: 'school', outlined: true, path: 'skoly'},
    schoolDetail: { text: 'Detail školy', icon: 'school', outlined: true, path: 'detail-skoly'},
    editSchoolDetail: { text: 'Upravit informace o škole', icon: 'edit', outlined: true, path: 'upravit-detail-skoly'},
    mainSchool: { text: 'Přehled', icon: 'home', outlined: true, path: 'prehled' },
    schoolProfile: { text: 'Profil školy', icon: 'school', outlined: true, path: 'profil-skoly' },
    editSchoolProfile: { text: 'Upravit profil školy', icon: 'edit', outlined: true, path: 'upravit-profil-skoly'},
    schoolCourses: { text: 'Naše kurzy', icon: 'date_range', outlined: true, path: 'nase-kurzy' },
    enlistCourse: { text: 'Rezervace kurzu', icon: 'event', outlined: true, path: 'prihlaseni-na-kurz' },
    schoolQuestionnaire: { text: 'Dotazník před kurzem', icon: 'event', outlined: true, path: 'dotaznik-pred-kurzem' },
    myFund: { text: 'Fond vzdělávání', icon: 'savings', outlined: true, path: 'fond-vzdelavani'},
    payouts: { text: 'Výpočet výplat', icon: 'payments', outlined: true, path: 'vypocet-vyplat'},
    diets: { text: 'Strava', icon: 'restaurant', outlined: true, path: 'strava'},
    accommondations: { text: 'Ubytování', icon: 'hotel', outlined: true, path: 'ubytovani'},
    notFound: { text: 'Chyba 404', icon: 'stop', outlined: false, path: '404-stranka-nenalezena'},
    notAuthorized: { text: 'Chyba 403', icon: 'stop', outlined: false, path: '403-stranka-nepristupna'},
};

export const pagesWithoutMenu = [ '/', '/'+pages.instructorLogin.path, '/'+pages.instructorRegister.path ];

export const api = {
    instructorLogin: target + 'login-api/login-instructor',
    instructorRegister: target + 'login-api/register-instructor',
    instructorLogout: target + 'login-api/logout',
    schoolLogin: target + 'login-api/login-school',
    schoolRegister: target + 'login-api/register-school',
    getLoggedUserData: target + 'login-api/get-logged-user-data',
    getAllInstructors: target + 'instructor-api/get-all-instructors',
    saveInstructorProfile: target + 'instructor-api/save-instructor-profile',
    createCourse: target + 'course-api/create-course',
    updateCourse: target + 'course-api/update-course',
    changeCourseState: target + 'course-api/change-course-state',
    updateCourseInstructors: target + 'course-api/update-course-instructors',
    saveInterest: target + 'course-api/save-interest',
    removeInterest: target + 'course-api/remove-interest',
    courseGetNearestFreeSpots: target + 'course-api/get-nearest-free-spots',
    courseGetActualYearCourses: target + 'course-api/get-actual-year-courses',
    courseGetMyCourses: target + 'course-api/get-my-courses',
    courseGetSchoolCourses: target + 'course-api/get-school-courses',
    courseGetInstructorCourses: target + 'course-api/get-instructor-courses',
    courseGetRequestedCourses: target + 'course-api/get-requested-courses',
    courseGetById: target + 'course-api/get-by-id',
    enlistNewCourse: target + 'course-api/enlist-new-course',
    enlistModifiedCourse: target + 'course-api/enlist-modified-course',
    createQuestionnaire: target + 'course-api/create-questionnaire',
    updateQuestionnaire: target + 'course-api/update-questionnaire',
    getFreeScopes: target + 'free-scopes-api/get-free-scopes',
    changeInstructorRole: target + 'instructor-api/change-instructor-role',
    getCourseThemes: target + 'course-theme-api/get-all-themes',
    themeGetById: target + 'course-theme-api/get-by-id',
    createTheme: target + 'course-theme-api/create-theme',
    updateTheme: target + 'course-theme-api/update-theme',
    getAllSchools: target + 'school-api/get-all-schools',
    schoolGetById: target + 'school-api/get-by-id',
    createSchool: target + 'school-api/create-school',
    updateSchool: target + 'school-api/update-school',
    getMyFund: target + 'fund-api/get-my-fund',
    getInstructorFund: target + 'fund-api/get-instructor-fund',
    createFundRecord: target + 'fund-api/create-fund-record',
    updateFundRecord: target + 'fund-api/update-fund-record',
    removeFundRecord: target + 'fund-api/remove-fund-record',
    createInstructorEvaluation: target + 'course-api/create-instructor-evaluation',
    updateInstructorEvaluation: target + 'course-api/update-instructor-evaluation',
    saveCourseReport: target + 'course-api/update-course-report'
};

export function getMenuItems(role: string): PageInfo[][] | null {

    switch (role) {
        case 'cekatel':
            return [[
                pages.mainNotApproved,
                pages.myProfile
            ]]
        case 'instruktor':
            return [[
                pages.main,
                pages.courses,
                pages.myCourses,
                pages.myProfile,
                pages.myFund
            ]]
        case 'zamestnanec':
            return [[
                pages.main,
                pages.courses,
                pages.myCourses,
                pages.myProfile,
                pages.myFund
            ],[
                pages.notifications,
                pages.allSchools,
                pages.instructors,
                pages.payouts,
                pages.allThemes,
                pages.diets,
                pages.accommondations
            ]]
        case 'skola':
            return [[
                //pages.mainSchool,
                pages.schoolProfile,
                pages.schoolCourses,
                pages.enlistCourse
            ]]
        default:
            return null;
    }
}

export function getAfterLoginPage(userType: string, role: string): any {
    if(userType == 'instructor') {
        if(role == 'blokovany') {
            return pages.mainBlocked;
        } else if(role == 'cekatel') {
            return pages.mainNotApproved;
        } else {
            return pages.main;
        }
    } else {
        return pages.schoolCourses;
    }
}

export function getPageInfoByUrl(url: string): PageInfo | undefined {
    var result: PageInfo | undefined = undefined;
    for (const key in pages)
    {
        var value = pages[key];
        if('/' + value.path == url) {
            result = value;
        }
    }
    return result;
}

export function canChangeRoleTo(userRole: string): string[] {
    switch (userRole) {
        case 'zamestnanec':
            return instructorRoles.slice(0, instructorRoles.indexOf('zamestnanec')); // muze menit pouze sve podrizene role
        case 'admin':
            return instructorRoles;
        default:
            return [];
    }
} 

export function roleIsAtLeast(userRole: string, minRole: string): boolean {
    var result = false;
    var index = instructorRoles.length;
    do {
        index--;
        if(instructorRoles[index] == userRole) {
            result = true;
        } 
    } while(instructorRoles[index] != minRole)
    return result;
} 

// year je rok, ve kterem pozadovany skolni rok zacina. Pri zadani null se bere aktualni skolni rok
export function getSchoolyear(year: number | null): any {
    var actualDate = new Date();
    if(year == null) {
        year = actualDate.getFullYear();
        if(actualDate.getMonth() < 9) {
            year--;// aktualni skolni rok zacina minulym rokem
        }
    }
    var result = {
        from: new Date(year,9,1),
        to: new Date(year+1,8,31),
    }
    return result;
}

export function lengthInUtf8Bytes(str: string) {
    // Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
    if(str == null) {
        return 0;
    }
    var m = encodeURIComponent(str).match(/%[89ABab]/g);
    return str.length + (m ? m.length : 0);
}

export function wordShape(count: number, word: string) {
    var result = '';
    if(count == 0) {
      switch (word) {
        case 'muz': return 'mužů'; break;
        case 'zena': return 'žen'; break;
        case 'kluk': return 'kluků'; break;
        case 'holka': return 'holek'; break;
        case 'kurz': return 'kurzů'; break;
        case 'instruktor': return 'instruktorů'; break;
        case 'ucitel': return 'učitelů'; break;
        case 'zak': return 'žáků'; break;
        default: return ''; break;
      }
    } else if (count == 1) {
      switch (word) {
        case 'muz': return 'muž'; break;
        case 'zena': return 'žena'; break;
        case 'kluk': return 'kluk'; break;
        case 'holka': return 'holka'; break;
        case 'kurz': return 'kurz'; break;
        case 'instruktor': return 'instruktor'; break;
        case 'ucitel': return 'učitel'; break;
        case 'zak': return 'žák'; break;
        default: return ''; break;
      }
    } else if (count < 5) {
      switch (word) {
        case 'muz': return 'muži'; break;
        case 'zena': return 'ženy'; break;
        case 'kluk': return 'kluci'; break;
        case 'holka': return 'holky'; break;
        case 'kurz': return 'kurzy'; break;
        case 'instruktor': return 'instruktoři'; break;
        case 'ucitel': return 'učitelé'; break;
        case 'zak': return 'žáci'; break;
        default: return ''; break;
      }
    } else {
      switch (word) {
        case 'muz': return 'mužů'; break;
        case 'zena': return 'žen'; break;
        case 'kluk': return 'kluků'; break;
        case 'holka': return 'holek'; break;
        case 'kurz': return 'kurzů'; break;
        case 'instruktor': return 'instruktorů'; break;
        case 'ucitel': return 'učitelů'; break;
        case 'zak': return 'žáků'; break;
        default: return ''; break;
      }     
    }
    return result;
}

export function areSameDay(date1: Date, date2: Date) {
    return date1.getFullYear() === date2.getFullYear() &&
        date1.getMonth() === date2.getMonth() &&
        date1.getDate() === date2.getDate();
}