//
//  ViewController.m
//  UnionSDKDemo
//
//  Created by syt on 15/3/20.
//  Copyright (c) 2015年 syt. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()  <LTUnionDelegate>

@end

@implementation ViewController
{

}

-(instancetype)init
{
    self = [super init];
    if (self) {
        [LTUnion sharedInstance].delegate = self;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //****************************************************************************
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    loginBtn.frame = CGRectMake(10, 40, 100, 40);
    [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(showLoginView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    //****************************************************************************
    UIButton *userCenterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    userCenterBtn.frame = CGRectMake(10, 90, 100, 40);
    [userCenterBtn setTitle:@"个人中心" forState:UIControlStateNormal];
    [userCenterBtn addTarget:self action:@selector(userCenter) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:userCenterBtn];
    
    UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    payBtn.frame = CGRectMake(120, 90, 100, 40);
    [payBtn setTitle:@"充值" forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(showPayView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:payBtn];
    
    //****************************************************************************
    UIButton *swithAccountBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    swithAccountBtn.frame = CGRectMake(10, 140, 100, 40);
    [swithAccountBtn setTitle:@"切换账号" forState:UIControlStateNormal];
    [swithAccountBtn addTarget:self action:@selector(switchAccount) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:swithAccountBtn];
    
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    logoutBtn.frame = CGRectMake(120, 140, 100, 40);
    [logoutBtn setTitle:@"注销" forState:UIControlStateNormal];
    [logoutBtn addTarget:self action:@selector(showLogoutView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logoutBtn];
    
    UIButton *showToolBarBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    showToolBarBtn.frame = CGRectMake(10, 190, 100, 40);
    [showToolBarBtn setTitle:@"显示悬浮按钮" forState:UIControlStateNormal];
    [showToolBarBtn addTarget:self action:@selector(showToolBar) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:showToolBarBtn];
    
    UIButton *hiddenToolBarBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    hiddenToolBarBtn.frame = CGRectMake(120, 190, 100, 40);
    [hiddenToolBarBtn setTitle:@"隐藏悬浮按钮" forState:UIControlStateNormal];
    [hiddenToolBarBtn addTarget:self action:@selector(hiddenToolBar) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:hiddenToolBarBtn];
    
    UIButton *reportEventBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    reportEventBtn.frame = CGRectMake(10, 230, 100, 40);
    [reportEventBtn setTitle:@"上报事件" forState:UIControlStateNormal];
    [reportEventBtn addTarget:self action:@selector(reportEvent) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:reportEventBtn];
    
    self.view.backgroundColor = [UIColor clearColor];

    [[LTUnion sharedInstance] LTUnionSDKInit];
    [LTUnion sharedInstance].delegate = self;
}
   
-(void)showLoginView{
    NSLog(@"Show Login View.");
    [[LTUnion sharedInstance] LTUnionSDKShowLoginView];
    [[LTUnion sharedInstance] LTUnionSDKEventReport:@"CREATE_ROLE" andExtension:@"asd"];
}

-(void)showLogoutView
{
    [[LTUnion sharedInstance] LTUnionSDKLogout];
}

-(void)showPayView
{
//    [[LTUnion sharedInstance] showPayMentView];
    
    LTProduct *ltProduct = [[LTProduct alloc] init];
    ltProduct.price = @"150";
    ltProduct.userID = self.entity.userID;
    ltProduct.roleID = @"test";
    ltProduct.itemID = @"com.longtugame.yulgang.l1";
    ltProduct.billTitle = @"充值测试150元";
    ltProduct.zoneID = @"1";
    ltProduct.payext = @"15_1_32";

    [[LTUnion sharedInstance] LTUnionSDKPay:ltProduct];

    [LTProduct release];
}

-(void)switchAccount
{
    [[LTUnion sharedInstance] LTUnionSDKSwitchAccount];
}

-(void)userCenter
{
    [[LTUnion sharedInstance] LTUnionSDKShowUserCenter];
}

-(void)showToolBar
{
    [[LTUnion sharedInstance] LTUnionSDKShowToolBar];
}

-(void)hiddenToolBar 
{
    [[LTUnion sharedInstance] LTUnionSDKHiddenToolBar];
}

-(void)reportEvent
{
    
}


#pragma mark -- LTUnionDelegate

-(void)LTUnionSDKInitCallBack:(int)code message:(NSString *)msg
{
    NSLog(@">>>>>>LTUnionSDKInitCallBack code = %d",code);

    if (code == 100) {
//        LTRoleInfo *roleInfo = [[LTRoleInfo alloc] init];
//        [[LTUnion sharedInstance] LTUnionSDKSendRoleInfo:roleInfo];
        [self showLoginView];
    }
    else{
        [[LTUnion sharedInstance] LTUnionSDKInit];
    }
}

- (void)LTUnionSDKLoginCallBack:(int)code message:(NSString *)msg entity:(LTEntity *)entity
{
    NSLog(@">>>>>>LTUnionSDK Login Callback entity ＝ %@",entity);
    
    self.entity = entity;
    if (code == 100) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"demo提示"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"确定"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)LTUnionSDKPayCallBack:(int)code message:(NSString *)msg
{
    NSLog(@">>>>>>LTUnionSDK Pay Callback code = %d, msg = %@",code,msg);
    if (code == 100) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"demo提示"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"确定"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)LTUnionSDKLogoutCallBack:(int)code message:(NSString *)msg{
    NSString *level = @"nu";
    [[LTUnion sharedInstance] LTUnionSDKAppsFlyerTrackEvent:[NSString stringWithFormat:@"af_level%@_achieved",level] withValues:nil];
    NSLog(@"LTUnionSDK Logout CallBack");
    [self showLoginView];
}

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

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    
}


@end
