classdef sleepQualityManager 
    %functions: 
    %           1.compute the sleep quality scores based on the sleep
    %   pattern.
    %           2.return the suggestions based on the scores computed.            
    % 
    
    properties (SetAccess = private)

    end
    properties(Constant)
        suggestions = { 'Congratulation! Your sleep quality is extremely high. Hope you keep your healthy lifestyle.';
                        'Your sleep quality is good, but you might find yourself in a degrading health condition. Please pay attention to it! Keep your bed a comfortable place for sleep, for a good place is the prerequisite of good sleep. Further, turn off radio as well as TV during sleep, keeping the bedroom a quiet place for rest.And remember never do other kinds of fun stuff like surfing the internet, playing with smartphone, in the bed----this helps your body form a potential idea that it is to sleep when get into bed.';
                        'Your sleep quality is relatively low, and you may suffer health problems due to lose of regularity in your daily life. Please follow a more regular daily schedule. Try to go to bed early, and do not stay up too late on weekends. Drink less coffee, eat less, especially before your bed time. Besides, you are highly recommended to do some exercises in the afternoon, which helps to achieve a peaceful sleep.';
                        'You must be alert since your sleep quality is really bad. You are suffering from a lack of sleep hours, and it does great harm to your health. If the few sleep hours are due to too heavy workload, please balance your time for working and sleeping. And if it¡¯s due to being not able to fall asleep, the most urgent thing you need to do is to form a sleep routine, which can be tooth brushing, locking the door, even setting an alarm clock, etc. You may also try taking a shower before sleep, do some respiratory relaxing movements or progressive muscle relaxations. Be cautious with sleeping pill, you should not always rely on it. If you find your situation even worse, go to see a doctor for advices is highly recommended.';
                         }
       
    end
    
    methods(Static)
        function score = getScore(pattern)
        numOfPattern = size(pattern,2);
        
        for i=1:numOfPattern
            
            undefined(i)= size(find(pattern{i}==0),2);
            
            awake(i) = size(find(pattern{i}==1),2);
            rem(i) = size(find(pattern{i}==2),2);
            light(i) = size(find(pattern{i}==3),2);
            deep(i) = size(find(pattern{i}==4),2);
            
        end
            
            
            total = undefined+awake+rem+light+deep;
       
            undefined = undefined./total;
            awake = awake./total;
            rem = rem./total;
            light = light./total;
            deep = deep./total;
            
            
            
            S_LIGHT = 2;
            S_REM = 3;
            S_DEEP = 4;
            S_AWAKE = 1;
            
           
         
            
            for i=1:numOfPattern
            
            if (light(i)<0.6 && light(i)>0.5)
                s_light = 2;
            else 
                s_light = 2 - (abs(light(i) - 0.55) - 0.05)*20;
            end
            
            if (rem(i)<0.25 && rem(i)>0.2)
                s_rem = 3;
            else
                s_rem = 3 - (abs(rem(i) - 0.225) - 0.025)*80;  
                
            end            
            
            if (deep(i)<0.20 && deep(i)>0.15)
                s_deep = 4;
            else if deep(i)>0.2  
                    s_deep = 4 + (deep(i)-0.2)*40;
                else
                    s_deep = 4 + (0.15-deep(i))*(-60);
                end
            end
            
            if (awake(i)<0.05)
                s_awake = 1;
            else
                s_awake = 1+(awake(i)-0.05)*(-20);
            end
            
            score(i) = 8*(s_light + s_rem +s_deep + s_awake);
            score(i)=(score(i)+100)/100;
            end
            
            
            
        end
   
        function  sug = getSuggestion(score)
            
            score = mean(score);
            
            S1 = 18;
            S2 = 17;
            S3 = 16;
            
            
            
            
            if score>S1
                sug = sleepQualityManager.suggestions{1};
            else if score>S2
                    sug = sleepQualityManager.suggestions{2};
            
                else if score>S3
                        sug = sleepQualityManager.suggestions{3};
                    else 
                        sug = sleepQualityManager.suggestions{4};
                    end
                end
            end
           
        end
     end
end

