/// <reference path="../../includes.ts" />

module app {

    import IFutureMedication = services.IFutureMedication;
    import IMedicationSearchResult = services.IMedicationSearchResult;
    import IPatientMedication = services.IPatientMedication;
    import IFutureMedicationCategory = services.IFutureMedicationCategory;

    class AlcoholResult {
        public tab: string;
        public color: string;
        constructor(
            public title: string,
            public description: string,
            public tabs: any[]) {}
    }

    class Controller {

        PatientMedications: any;
        LoadingInProgress = false;
        medicinesrch: any;
        Medicines: any;
        keyToRemove: any;
        Medication: any;
        FutureMedications: IFutureMedication;
        Disclaimer: string;
        defaultMedicationID: string;
        selectedMedicines: IFutureMedicationCategory;
        loadCount: number = 0;


        public selected: any;
        public addingMed: boolean = false;
        public loaded: boolean = false;
        public navigation: any[] = this.medicationService.navigation();

        public medsTabs = [
            {value: 'mine', label: 'My Medications', title: 'Add to My Medications'},
            {value: 'heart', label: 'Heart', title: 'Heart Medications'},
            {value: 'mental', label: 'Mental Health', title: 'Mental Health Medications'},
            {value: 'pain', label: 'Pain', title: 'Pain Medications'},
            {value: 'reflux', label: 'Reflux', title: 'Reflux Medications'},
            {value: 'other', label: 'Other', title: 'Other Medications'},
        ];
        public selectedTab: any = this.medsTabs[0];
        public selectedView: string = 'mine';

        static $inject = [
            '$scope',
            '$sce',
            'medicationService',
            'modalService',
            'user'
        ];
        constructor(
            public $scope: angular.IScope,
            private $sce: angular.ISCEService,
            private medicationService: services.IMedicationService,
            private modalService: IModalService,
            public user: services.IUser
        ) {
            this.getFutureMedications();
            this.getPatientMedications();

            $scope.$on('medsTabChanged', (ev: angular.IAngularEvent, tab: any) => {
                ev.stopPropagation();
                this.selectedTab = tab;
                this.selectedView = tab.value;
                if (tab.value !== 'mine') {
                    this.selectedMedicines = this.FutureMedications.Categories.filter((fm: IFutureMedicationCategory) => {
                        return fm.Name === tab.title;
                    })[0]
                } else {
                    this.selectedMedicines = null;
                }
            })
        }

        medsSearch() {
            if (this.medicinesrch.length > 2) {
                this.LoadingInProgress = true;
                this.medicationService.search(this.medicinesrch).then((values: IMedicationSearchResult[]) => {
                    this.Medicines = values;
                    this.LoadingInProgress = false;
                });
            } else {
                this.Medicines = null;
            }
        }

        addMedication(itm: any) {
            console.log(itm);
            this.addingMed = true;
            this.medicationService.addMedication(itm.id).then((value: IPatientMedication[]) => {
                this.PatientMedications = value;
                this.addingMed = false;

            });
            this.medicinesrch = null;
            this.Medicines = null;
        }

        removeMedicine(item: IPatientMedication, medIndex: number) {
            this.modalService.add('modal-notification', {
                title: 'Are you sure?',
                content: `This will remove&nbsp;<b>${item.Medication.DisplayName}</b>&nbsp;from your medications.`,
                buttons: ['Ok', {label: 'Cancel', className: 'secondary'}]
            }).then((index: number) => {
                if (index === 0) {
                    this.medicationService.removeMedication(item.Id, medIndex).then((meds: IPatientMedication[]) => {
                        this.PatientMedications = meds;
                    })
                }
            })

        }

        getPatientMedications() {
            this.medicationService.medications().then((data: IPatientMedication[]) => {
                if (data.length > 1 && data[0].Id == this.defaultMedicationID) {
                    data.splice(0, 1);
                }
                this.PatientMedications = data;
                this.loadCount++;
            });
        }

        showDetails(item: any){
            this.modalService.add('modal-notification', {
                title: item.PredictedMetabolism,
                content: item.Comment,
            });
        }

        showMedDetails(id: string, alertType: string, category: string){
            this.medicationService.medication(id).then((data: IPatientMedication) => {
                let med: any = data.Medication;
                this.modalService.add('modal-notification', {
                    title: med.DisplayName,
                    content: `
						<p>${med.CommonUsage}</p>
						<div class="title">Impact of test results:</div>
						<p>${data.Comment}</p>`
                });
            });
        }

        highlightSearchParam(value: string) {

            return this.$sce.trustAsHtml(value);
        }

        getFutureMedications() {
            this.medicationService.future().then((data: IFutureMedication) => {
                this.FutureMedications = data;
                this.loadCount++;
            });
        }

    }

    angular
        .module('app')
        .controller('medsMedicationsCtrl', Controller);
}