//
//  ViewController.m
//  iOSJSBridgeDemo
//
//  Created by 罗坚(EX-LUOJIAN004) on 2018/4/23.
//  Copyright © 2018年 罗坚(EX-LUOJIAN004). All rights reserved.
//

#import "ViewController.h"
#import "WebViewController.h"
#import "WKWebViewController.h"
#import "CameraPlugin.h"

@interface ViewController ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) CameraPlugin *cameraPlugin;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"App Core JSBridge";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setTitle:@"UIWebView" forState:UIControlStateNormal];
    [btn1 setBackgroundColor:[UIColor lightGrayColor]];
    [btn1 setFrame:(CGRect){40,100,100,40}];
    [btn1.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    [btn1 addTarget:self action:@selector(ClickBtn1) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitle:@"WKWebView" forState:UIControlStateNormal];
    [btn2 setBackgroundColor:[UIColor lightGrayColor]];
    [btn2 setFrame:(CGRect){self.view.bounds.size.width-40-100,100,100,40}];
    [btn2.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    [btn2 addTarget:self action:@selector(ClickBtn2) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn3 setTitle:@"原生调用相机插件" forState:UIControlStateNormal];
    [btn3 setBackgroundColor:[UIColor lightGrayColor]];
    [btn3 setFrame:(CGRect){40,150,140,40}];
    [btn3.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    [btn3 addTarget:self action:@selector(ClickBtn3) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRect){40, 200, 100, 100}];
    self.imageView = imageView;
    
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    [self.view addSubview:btn3];
    [self.view addSubview:self.imageView];
}

- (void)ClickBtn1
{
    WebViewController *vc = [[WebViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)ClickBtn2
{
    WKWebViewController *vc = [[WKWebViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)ClickBtn3
{
    CameraPlugin *plugin = [[CameraPlugin alloc] init];
    plugin.vc = self;
    __weak typeof(self) ws = self;
    plugin.nativeResponseBlock = ^(ACMessage *msg) {
        if (msg.responseCode == AppCoreResponseSuccess) {
            NSString *base64Str = msg.responseDic[@"base64Image"];
            NSData * imageData =[[NSData alloc] initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
            UIImage *image = [UIImage imageWithData:imageData];
            [ws.imageView setImage:image];
        }
    };
    ACMessage *msg = [[ACMessage alloc] init];
    [plugin showCameraMenu:msg];
    
    self.cameraPlugin = plugin;
}

@end
