//
//  Utilities.m
//  LifeLog
//
//  Created by Nguyen Van Phong on 7/29/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import <Social/Social.h>
#import <LineKit/Line.h>

#import "Utilities.h"
#import "ServerAPI.h"

@implementation Utilities
+ (NSString *)addCommaFromNumber:(NSInteger)number
{
    NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
    [fmt setNumberStyle:NSNumberFormatterDecimalStyle];
    [fmt setMaximumFractionDigits:0];
    NSString *result = [fmt stringFromNumber:@(number)];
    return result;
}

+ (UIColor *)convertHecToColor:(int) hex
{
    return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 \
                           green:((float)((hex & 0xFF00) >> 8))/255.0 \
                            blue:((float)(hex & 0xFF))/255.0 alpha:1.0];
}

+ (void)showMessage:(NSString *)message withViewController:(UIViewController *)vc
{
    if (message.length > 0) {
        UIAlertController * alert=   [UIAlertController
                                      alertControllerWithTitle:@"Message"
                                      message:message
                                      preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* ok = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 [alert dismissViewControllerAnimated:YES completion:nil];
                             }];
        
        [alert addAction:ok];
        
        [vc presentViewController:alert animated:YES completion:nil];
    }
}

+ (void)showErrorMessage:(NSString *)message withViewController:(UIViewController *)vc
{
  if (message.length > 0) {
    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:@"Error"
                                  message:message
                                  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* ok = [UIAlertAction
                         actionWithTitle:@"OK"
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                           [alert dismissViewControllerAnimated:YES completion:nil];
                         }];
    
    [alert addAction:ok];
    
    [vc presentViewController:alert animated:YES completion:nil];
  }
}

+ (NSString *) getImageLink : (NSString *) path {
    NSString * link = kServerAddress;
    return [link stringByAppendingString:path];
}

+ (void) shareFacebook : (NSString *) content withViewController:(UIViewController *)vc {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        
        [composeViewController setInitialText:content];
        [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) {
            
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"canceled");
                    break;
                case SLComposeViewControllerResultDone:
                    NSLog(@"done");
                    break;
                default:
                    break;
            }
        }];
        [vc presentViewController:composeViewController animated:YES completion:nil];
    }
    else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=FACEBOOK"]];
    }
}

+ (void) shareTwitter : (NSString *) content withViewController:(UIViewController *)vc {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        
        [composeViewController setInitialText:content];
        [composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) {
            
            switch (result) {
                case SLComposeViewControllerResultCancelled:
                    NSLog(@"canceled");
                    break;
                case SLComposeViewControllerResultDone:
                    NSLog(@"done");
                    break;
                default:
                    break;
            }
        }];
        [vc presentViewController:composeViewController animated:YES completion:nil];
    }
    else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=TWITTER"]];
    }
}

+ (void) shareLine : (NSString *) content withViewController:(UIViewController *)vc {
    if (![Line isLineInstalled]) {
        [self showErrorMessage:@"Install Line app first" withViewController:vc];
    }
    else {
        [Line shareText:content];
    }
}

+ (void) shareEmail : (NSString *) content withViewController:(UIViewController *)vc {
    NSString *urlEmail = @"mailto:?subject=Share from LifeLog&body=";
    urlEmail = [urlEmail stringByAppendingString:content];
    urlEmail = [urlEmail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [[UIApplication sharedApplication]  openURL: [NSURL URLWithString: urlEmail]];
}

+ (void) shareOther : (NSString *) content withViewController:(UIViewController *)vc {
    NSArray *items = @[content];
    UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
    [vc presentViewController:controller animated:true completion:nil];
}

#pragma mark convert date time
+ (NSDate *) dateFromString : (NSString *) dateString withFormat: (NSString *) format {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:format];
    NSDate *date  = [dateFormat dateFromString:dateString];
    if(date == NULL) {
        return [NSDate date];
    }
    return date;
}

+ (NSString *) stringFromDate : (NSDate *) date withFormat: (NSString *) format locale:(NSString *) locale {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:format];
    if(![locale  isEqual: @""]) {
        [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:locale]];
    }
    NSString *dateString  = [dateFormat stringFromDate:date];
    return dateString;
}

+ (NSString *) convertSecondToShortTime : (int) seconds {
    int hour = seconds / 3600;
    int minutes = (seconds - hour * 3600) / 60;
    return [NSString stringWithFormat:@"%02d:%02d", hour, minutes];
}

+ (NSString *) convertSecondToLongTime : (int) seconds {
    int hour = seconds / 3600;
    int minutes = (seconds - hour * 3600) / 60;
    int sec = seconds - hour * 3600 - minutes * 60;
    return [NSString stringWithFormat:@"%02d:%02d:%02d", hour, minutes, sec];
}

@end
