//
//  ViewController.m
//  TwitterShare
//
//  Created by Donald Patterson on 10/7/15.
//  Copyright © 2015 Donald J. Patterson. All rights reserved.
//

#import "ViewController.h"
#import "Social/Social.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *tweetTextView;


- (void) showAlertMessage:(NSString *) myMessage;
- (void) configureTweetTextView;


@end

@implementation ViewController

- (void) showAlertMessage:(NSString *) myMessage{
    UIAlertController *alertController;
    alertController = [UIAlertController alertControllerWithTitle:@"SocialShare"
                                                          message:myMessage
                                                   preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"Okay"
                                                        style:UIAlertActionStyleDefault
                                                      handler:nil]
     
     ];
    
    [self presentViewController:alertController animated:YES completion:nil];
}


- (void) configureTweetTextView{
    self.tweetTextView.layer.backgroundColor = [UIColor colorWithRed:1.0
                                                               green:0.9 blue:0.9 alpha:1.0].CGColor;
    
    self.tweetTextView.layer.cornerRadius = 10.0;
    self.tweetTextView.layer.borderColor = [UIColor colorWithWhite:0 alpha:0.5].CGColor;
    self.tweetTextView.layer.borderWidth = 2.0;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureTweetTextView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)tweetAction:(id)sender {
    
    if([self.tweetTextView isFirstResponder]){
        [self.tweetTextView resignFirstResponder];
    }
    
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *twitterComposeVC =
        
        [SLComposeViewController composeViewControllerForServiceType: SLServiceTypeTwitter];
        
        if ([self.tweetTextView.text length] <= 140) {
            [twitterComposeVC setInitialText:self.tweetTextView.text];
        }
        else {
            NSString *subText = [self.tweetTextView.text substringToIndex:140];
            [twitterComposeVC setInitialText:subText];
        }
        [self presentViewController:twitterComposeVC animated:YES completion:nil];
        
    }
    else {
        [self showAlertMessage:@"You are not logged in to your Twitter account."];
    }


    
}

@end
