//
//  DTGPSButton.m
//  DingTalkDylib
//
//  Created by 耿建峰 on 17/1/4.
//
//

#import "DTGPSButton.h"

#import "DTGPSSettingViewController.h"

#import "DTWifiSettingViewController.h"

#define DTGPS_SETTING_CENTER_X       @"DTGPS_SETTING_CENTER_X"
#define DTGPS_SETTING_CENTER_Y       @"DTGPS_SETTING_CENTER_Y"
#define DTGPS_SETTING_DURATION 0.2

static NSString *image_base64 = @"iVBORw0KGgoAAAANSUhEUgAAADgAAAA4CAYAAACohjseAAAAAXNSR0IArs4c6QAA\
AAlwSFlzAAAWJQAAFiUBSVIk8AAAABxpRE9UAAAAAgAAAAAAAAAcAAAAKAAAABwA\
AAAcAAAEmbsfYzsAAARlSURBVGgFzFhbbxNHFLaEBKgSvPCExGPVn9AX+AuoT6iv\
PPUF1uZS4nUKlQlBkBaKIDE0CdCmVVsEvXBRgIcQ2+sAKSRuuLXl4sR24txF7DgJ\
TurkcM6Q2Y6d3eyMYzATrWf2zDnfOd+c3TOTdbnK3LZd3LaqytA+1sOevXpIa8Hr\
Dl5JPeTO4AWLF45JxuZaSJdsyLbM4ZQHTruurcEgP/WGPL/pIU9aIMIJSfaeNMNA\
LMIsT3QrQPG17djgC3kOIqHx0kmZWS1ehHHCJh8rCLE0U3/XZx/oQXctksq+BWLF\
RLPki3yWFq2ilTekbUVS8XdArJhognwrhiuv7g9uX6uHtfoKECsg6gu6GygW+cgl\
NKuNXRuRWLTS5AT/UYpJInRnFV9kx0cI3CeAF6xoBeV9FJszg2U09ODuD7Hsj1SQ\
hMNiYmwY4zIU7KeqgtomJFaJYuJAasm2kqBY7ZlYzLCN+/1655xIR5UOBpi5wPv7\
WC7JICcfsMjVUpG33f1JOcjhKYQ7Bn+HF3h79d+MKRf9iPqiXGVMsS9lJEiqOqrW\
IWBKBbRY91TXVxCbeA4/P/nOJOJE8PzDM5DI9EGg+7hpU4wreZ/yd2rrBUqFQ19I\
OywJZBnID4+bYQH/qE3kXkJTTz0YyVsQz/TyBLI+k0tD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3G1z13G2f+5Wd\
K/2qp38Oi/wOjrYFGRnKtdtXUYGhOSgyJCs7VhtxFp7L13KWU4Etc0hkCzgJC7Fy\
CChbF/qD0qn/LVJW3CXgYsU1qzX6GmADFZnRi6osL04XYmdF3q4zSrIc39ZlvHcV\
EBtBrNxHuYvt8nPMTqYbdOzc8f5RgLCtnwdgMmwawzHCmQLhvxf+YPpUzw0qdDgL\
scPAANZf/+cyJee035brW+bladKaxNu6SrpZqE9sGEYTwmO0H6Tfzviau5IWjly5\
xrMWvk7z+QNd4KXDAWbfsQAAAABJRU5ErkJggg==";

@interface DTGPSButton () {
    
    CGPoint startPoint;
    CGPoint originPoint;
}

@property (nonatomic, strong) UIButton *button_gps;

@end

@implementation DTGPSButton

+ (instancetype)sharedInstance {
    
    static DTGPSButton *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[DTGPSButton alloc] init];
    });
    
    return instance;
}

- (instancetype)init {
    
    if(self = [super init]) {
        
        self.userInteractionEnabled = YES;
        
        NSData *decodedImageData   = [[NSData alloc] initWithBase64EncodedString:image_base64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
        UIImage *decodedImage      = [UIImage imageWithData:decodedImageData];
        
        // add button
        self.button_gps = [UIButton buttonWithType:UIButtonTypeCustom];
        _button_gps.frame = CGRectMake(0, 0, 40, 40);
        [_button_gps addTarget:self action:@selector(gotoSettingView:) forControlEvents:UIControlEventTouchUpInside];
        [_button_gps setBackgroundImage:decodedImage forState:UIControlStateNormal];
        [self addSubview:_button_gps];
        
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)];
        [self addGestureRecognizer:longGesture];
        
        CGFloat center_x = [[NSUserDefaults standardUserDefaults] floatForKey:DTGPS_SETTING_CENTER_X];
        CGFloat center_y = [[NSUserDefaults standardUserDefaults] floatForKey:DTGPS_SETTING_CENTER_Y];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if(center_x > 0 && center_y > 0) {
            
            self.center = CGPointMake(center_x, center_y);
        }
    }
    
    return self;
}

- (void)gotoSettingView:(id)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"位置设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        DTGPSSettingViewController *viewController = [[DTGPSSettingViewController alloc] initWithNibName:nil bundle:nil];
        
        
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:navigationController animated:YES completion:nil];
    }]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"WIFI设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        DTWifiSettingViewController *viewController = [[DTWifiSettingViewController alloc] initWithNibName:nil bundle:nil];
        
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
        
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:navigationController animated:YES completion:nil];
    }]];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

- (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender {
    
    UIView *view = sender.view;
    
    if (sender.state == UIGestureRecognizerStateBegan) {
        
        startPoint = [sender locationInView:sender.view];
        originPoint = view.center;
        [UIView animateWithDuration:DTGPS_SETTING_DURATION animations:^{
            
            view.transform = CGAffineTransformMakeScale(1.1, 1.1);
            view.alpha = 0.7;
        }];
        
    }
    else if (sender.state == UIGestureRecognizerStateChanged) {
        
        CGPoint newPoint = [sender locationInView:sender.view];
        
        CGFloat deltaX = newPoint.x - startPoint.x;
        CGFloat deltaY = newPoint.y - startPoint.y;
        
        originPoint = CGPointMake(view.center.x + deltaX, view.center.y + deltaY);
        
        if(originPoint.x <= (view.bounds.size.width / 2)) {
            originPoint.x = view.bounds.size.width / 2;
        }
        else if(originPoint.x >= ([self superview].frame.size.width - (view.bounds.size.width / 2))) {
            originPoint.x = [self superview].frame.size.width - (view.bounds.size.width / 2);
        }
        if(originPoint.y <= (view.bounds.size.height / 2)) {
            originPoint.y = view.bounds.size.height / 2;
        }
        else if(originPoint.y >= ([self superview].frame.size.height - (view.bounds.size.height / 2))) {
            originPoint.y = [self superview].frame.size.height - (view.bounds.size.height / 2);
        }
        
        view.center = originPoint;
    }
    else if (sender.state == UIGestureRecognizerStateEnded) {
        
        [UIView animateWithDuration:DTGPS_SETTING_DURATION animations:^{
            
            view.transform = CGAffineTransformIdentity;
            
            view.alpha = 1.0;
        }];
        
        [[NSUserDefaults standardUserDefaults] setFloat:originPoint.x forKey:DTGPS_SETTING_CENTER_X];
        [[NSUserDefaults standardUserDefaults] setFloat:originPoint.y forKey:DTGPS_SETTING_CENTER_Y];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

@end
