//
//  RootViewController.m
//  Lemon_iOS_SDK_Demo
//
//  Created by syt on 14-3-31.
//  Copyright (c) 2014年 syt. All rights reserved.
//

#import "RootViewController.h"

#import "LMGameStartScene.h"

#import <KakaoGame/KakaoGame.h>

@implementation RootViewController
{
    LMGameStartScene *gameStartScene;
}

@synthesize items = _items;
@synthesize entity = _entity;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.items = IOS_ITEMS;
    
    
    //第一步 设置Lemon代理
    [LMPlatform sharedInstance].lmdelegate = self;
    [[LMPlatform sharedInstance]init:CODENAME productID:CODENUMBER debug:NO];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}


#pragma mark - LMDelegate Method
//初始化回调
- (void)lemonSDKInitCallBack:(int)code msg:(NSString *)msg
{
    LMDebugS(@"Init Callback Code = %d,Message = %@",code,msg);    
    
    [SVProgressHUD dismiss];
    
    if (code == 0) {
        LMDebugS(@"Init Callback SUCCESS");
        [[LMPlatform sharedInstance]showLemonProtocolView:YES];
    }
    else
    {
        LMDebugS(@"Init Callback FAILED");
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:[NSString stringWithFormat:@"%@，请重试",msg]
                                                           delegate:self
                                                  cancelButtonTitle:@"重试"
                                                  otherButtonTitles:nil];
        alertView.tag = 100;
        [alertView show];
    }
}



//用户协议全部同意回调
- (void)agreeProtocolCallBack
{
    LMDebugS(@"用户协议全部同意");
    
    NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"LemonSDKResources" ofType:@"bundle"]];
    
//    NSString *path = [bundle pathForResource:@"LemonLogoHorizontal" ofType:@"mp4"];
    
//    [[LMPlatform sharedInstance] showLogoVideoWithPath:path backgroundColor:[UIColor redColor] andDelegate:self];
    [[LMPlatform sharedInstance] lemonLoginSystem];
}

//视屏播放完毕回调
-(void)videoFinishCallBack
{
    LMDebugS(@"视屏播放完毕");
}

//SDK登陆系统登录后回调此方法
- (void)loginPopupCallBack:(NSString *)loginSystemOperation Code:(int)code msg:(NSString *)msg entity:(LMEntity *)entity
{
    LMDebugS(@"loginSystemOperation = %@",loginSystemOperation);
    LMDebugS(@"LMEntity = %@",entity);
    
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
//                                                        message:msg
//                                                       delegate:self
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:nil];
//    alertView.tag = 101;
//    [alertView show];
    
    if(code == 0){
        LMDebugS(@"Demo loginPopupCallBack %@ success msg = %@",loginSystemOperation,msg);
        self.entity = entity;
        
        gameStartScene = [[LMGameStartScene alloc] initWithFrame:self.view.bounds rootViewController:self];
        
//        [[LMPlatform sharedInstance] sendReward];
        
    }
    else if(code == 1)
    {
        for (UIView *view in self.view.subviews) {
            if([view isKindOfClass:[UIImageView class]]==NO)
                [view removeFromSuperview];
        }
    }
    else{
        LMDebugS(@"Demo loginPopupCallBack %@ failed msg = %@",loginSystemOperation,msg);
    }
}

- (void)cancellationOfTheCurrentAccountCallBack:(NSString *)msg
{
    for (UIView *view in self.view.subviews) {
        if([view isKindOfClass:[UIImageView class]]==NO)
            [view removeFromSuperview];
    }
    
    [[LMPlatform sharedInstance]showLemonProtocolView:YES];
    
    LMDebugS(@"cancellationOfTheCurrentAccountCallBack msg = %@",msg);
}
-(void)loginCallBack:(int)code msg:(NSString *)msg entity:(LMEntity *)entity
{
    LMDebugS(@"%@",entity);
    if(code == 0)
    {
        
    }
    else
    {
    
        if (code == 401 || code == 4001) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                                message:msg
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            [alertView show];
        }
        
        LMDebugS(@"登录失败 code = %d  msg = %@ entity = %@ ",code,msg,entity);
    }
}

-(void)autoRegistCallBack:(int)code msg:(NSString *)msg entity:(LMEntity *)entity
{
    LMDebugS(@"%@",entity);
    
    if(code == 0)
    {
        
    }
    else
    {
        [self showAlertView:@"快速登录失败!"];
    }
}



- (void)purchaseCallBack:(int)code msg:(NSString *)msg
{
    LMDebugS(@"IAP Purchase SUCCESS Callback code = %d,msg = %@",code,msg);
    [self showAlertView:msg];
}

- (void)failedPurchaseCallBack:(int)code msg:(NSString *)msg
{
    LMDebugS(@"IAP Purchase FAILED Callback code = %d,msg = %@",code,msg);
    [self showAlertView:msg];
}


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

- (void)kakaoMemberKeyCallBack:(NSString *)key
{
    NSLog(@"Member key = %@",key);
    [self showAlertView:key];
}

- (void)logoutKakaoCallBack:(int)code msg:(NSString *)msg
{
    LMDebugS(@"登出kakao code = %d  msg = %@",code,msg);
//
    if (code == 0) {
        for (UIView *view in self.view.subviews) {
            if([view isKindOfClass:[UIImageView class]]==NO)
                [view removeFromSuperview];
        }
         [[LMPlatform sharedInstance]init:CODENAME productID:CODENUMBER debug:NO];
//        [self showAlertView:msg];
    }
}

- (void)deleteLogoutKakaoCallBack:(int)code msg:(NSString *)msg
{
    LMDebugS(@"删除kakao用户 code = %d  msg = %@",code,msg);
    
    if(code == 0)
    {
        for (UIView *view in self.view.subviews)
        {
            if([view isKindOfClass:[UIImageView class]]==NO)
                [view removeFromSuperview];
        }
        
        [[LMPlatform sharedInstance] showLemonProtocolView:YES];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                            message:[NSString stringWithFormat:@"code=%d,msg=%@",code,msg]
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        alertView.tag = 101;
        [alertView show];
    }
}

- (void)getGameFriendListCallBack:(int)code msg:(NSString *)msg friends:(NSArray *)friends
{
    LMDebugS(@"获取游戏朋友列表code = %d  msg = %@ friends = %@",code,msg,friends);
    [self showAlertView:[NSString stringWithFormat:@"获取游戏朋友列表code = %d  msg = %@ friends = %@",code,msg,friends]];
}

//获取kakao朋友列表回调
- (void)getkakaoFriendCallBack:(int)code msg:(NSString *)msg friends:(NSArray *)friends
{
    LMDebugS(@"获取kakao朋友列表kakao用户 code = %d  msg = %@ friends =%@",code,msg,friends);
    [self showAlertView:[NSString stringWithFormat:@"获取kakao朋友列表kakao用户 code = %d  msg = %@ friends =%@",code,msg,friends]];
}

-(void)getFiveRocksProductIdCallBack:(NSString *)productId
{
    [self showAlertView:productId];
    NSLog(@"productId = %@",productId);
}

-(void)sendRewardRequstCallBack:(int)code msg:(NSString *)msg
{
    LMDebugS(@"获取报酬 code = %d  msg = %@ ",code,msg);
    [self showAlertView:[NSString stringWithFormat:@"获取报酬 code = %d  msg = %@ ",code,msg]];
}

//- (void)getkakaoInvitableCallBack:(int)code msg:(NSString *)msg friends:(NSArray *)friends
//{
//    LMDebugS(@"获取邀请好友code = %d  msg = %@ friends = %@",code,msg,friends);
//
//    KGKakaoProfile *friendEntity = [friends objectAtIndex:1];
//
//    [[LMPlatform sharedInstance] sendTemplateMessageWithFriendEntity:friendEntity];
//    [self showAlertView:[NSString stringWithFormat:@"获取邀请好友code = %d  msg = %@ friends = %@",code,msg,friends]];
//}

- (void)getkakaoInvitableCallBack:(int)code msg:(NSString *)msg nickName:(NSString *)nickName uuid:(NSString *)uuid{
    LMDebugS(@"获取邀请好友code = %d  msg = %@ nickName = %@ uuid = %@",code,msg,nickName,uuid);
    
    [self showAlertView:[NSString stringWithFormat:@"获取邀请好友 code = %d  msg = %@ nickName = %@ uuid = %@",code,msg,nickName,uuid]];
}

- (void)sendTemplateMessageCallBack:(int)code msg:(NSString *)msg friends:(KGKakaoProfile *)friends
{
    LMDebugS(@"发送邀请信息code = %d  msg = %@ friend = %@ ",code,msg,friends);
    [self showAlertView:[NSString stringWithFormat:@"发送邀请信息code = %d  msg = %@ friend = %@ ",code,msg,friends]];
}

-(void)getKakaoInvitationCountCallBack:(int)code count:(int)count
{
    LMDebugS(@"受我邀请进入游戏的好友数量 code = %d count = %d",code,count);
    [self showAlertView:[NSString stringWithFormat:@"受我邀请进入游戏的好友数量 code = %d count = %d",code,count]];
}

-(void)gameCenterUploadScoreFinish
{
    LMDebugS(@"gameCenterUploadScoreFinish");
}

-(void)gameCenterUploadScoreFailWithLeaderboardIdentifier:(NSString *)leaderboardIdentifier andScore:(int64_t)score
{
    LMDebugS(@"leaderboardIdentifier= %@,score =%lld",leaderboardIdentifier,score);
    
}

-(void)gameCenterUploadAchievementFinish
{
    LMDebugS(@"gameCenterUploadAchievementFinish");
}

-(void)gameCenterUploadAchievementFailWithAchievement:(NSString *)achievement andPercentComplete:(double)percentComplete
{
    LMDebugS(@"achievement = %@,percentComplete = %f",achievement,percentComplete);
}

-(void)gameCenterOpenViewController
{
    LMDebugS(@"gameCenterOpenViewController");
}

-(void)gameCenterCloseViewController
{
    LMDebugS(@"gameCenterCloseViewController");
}


//对应接口网络请求失败或者超时会回调此方法
- (void)networkRequestFailedCallBack
{
    [SVProgressHUD dismiss];
    LMDebugS(@"网络异常，请检查网路链接");
}


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [alertView removeFromSuperview];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 100) {
        [[LMPlatform sharedInstance]init:CODENAME productID:CODENUMBER debug:NO];
    }
}

-(void)showAlertView:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
    [alertView show];
}

@end
