/****************************************************************************
Copyright (c) 2013, haowan123
文件：PlatformInterface.h

****************************************************************************/
#import "PlatformInterface_IOS.h"
#import "PlatformInterface.h"
#import <FuncellPlatform/FuncellPlatform.h>

using namespace std;

/**
 *获取哦服务器列表
 *whitekey
 白名单标识,没有可以传“”
 */
void CPlatformInterface_IOS::getServerList(const char* whitekey)
{
    [[FunCellPlatform sharedInstance] GetServerList:[NSString stringWithUTF8String:whitekey]];
}

/**
 *获取公告列表
 *type
 公告类型,参考文档
 serverid
 区服id,可以传""
 */
void CPlatformInterface_IOS::getNoticeList(const char* type, const char* serverid)
{
    [[FunCellPlatform sharedInstance] GetNoticeList:[NSString stringWithUTF8String:type] andserverid:[NSString stringWithUTF8String:serverid]];
}

//----------------------C 调用的接口-------------------------
/**
 *登陆
 */
void CPlatformInterface_IOS::Login()
{
    [[FunCellPlatform sharedInstance] Login];
}

/**
 *支付
 */
void CPlatformInterface_IOS::Pay( const char * itemid, const char * itemName, const char * type,int num ,float money, const char* currency, const char *extData,const char *paysdktype)
{
    NSString* nsitemid = [NSString stringWithUTF8String:itemid];
    NSString* nsitemname = [NSString stringWithUTF8String:itemName];
    NSString* nstype = [NSString stringWithUTF8String:type];
    NSString* nscurrency = [NSString stringWithUTF8String:currency];
    NSString* nsextData = [NSString stringWithUTF8String:extData];
    NSString* nspaysdktype = [NSString stringWithUTF8String:paysdktype];
    [[FunCellPlatform sharedInstance] Pay:nsitemid anditemName:nsitemname andtype:nstype andnum:num andmoney:money andcurrency:nscurrency andextdata:nsextData andpaysdktype:nspaysdktype];
}

/**
 *获取支付列表
 */
void CPlatformInterface_IOS::GetPayList()
{
    [[FunCellPlatform sharedInstance] GetPayList];
}

/**
 *获取未完成订单
 */
void CPlatformInterface_IOS::GetOrderList()
{
    [[FunCellPlatform sharedInstance] GetOrderList];
}

/**
 *游戏处理完充值订单后反馈平台
 */
void CPlatformInterface_IOS::PayFeedback(const char * billNo)
{
    NSString* nsbillNo = [NSString stringWithUTF8String:billNo];
    [[FunCellPlatform sharedInstance] PayFeedback:nsbillNo];
}

/**
 *游戏通知平台退出登录
 */
void CPlatformInterface_IOS::GameToPlatformLogout()
{
    [[FunCellPlatform sharedInstance] GameToPlatformLogout];
}

/**
 *设置区号areaID和区名areeaName
 */
void CPlatformInterface_IOS::SetAreaInfo(const char * areaID, const char * areaName)
{
    NSString* nsareaid = [NSString stringWithUTF8String:areaID];
    NSString* nsareaname = [NSString stringWithUTF8String:areaName];
    [[FunCellPlatform sharedInstance] SetAreaInfo:nsareaid andAreaName:nsareaname];
}

/**
 *设置角色信息
 *rolename:角色名
 *roleid:角色id
 *rolelevel:角色等级
 *profession:职业
 *power:战力
 *gold:财富
 */
void CPlatformInterface_IOS::SetRoleInfo(const char * roleName, const char * roleID, int rolelevel, const char *  Profession, int Power, int Gold)
{
    NSString* nsrolename = [NSString stringWithUTF8String:roleName];
    NSString* nsroleid = [NSString stringWithUTF8String:roleID];
    NSString* nsprofession = [NSString stringWithUTF8String:Profession];
    [[FunCellPlatform sharedInstance] SetRoleInfo:nsrolename androleID:nsroleid androlelevel:rolelevel andProfession:nsprofession andPower:Power andGold:Gold];
}

//////////////获取平台参数
/**
 *获取平台编号
 */
const int CPlatformInterface_IOS::GetPlatformID()
{
    return [[FunCellPlatform sharedInstance] GetPlatformID];
}

/**
 *获取平台名字
 */
const char * CPlatformInterface_IOS::GetPlatformName()
{
    return [[[FunCellPlatform sharedInstance] GetPlatformName] UTF8String];
}

/**
 *获取发现服返回结果
 */
const char * CPlatformInterface_IOS::GetEveData()
{
    return [[[FunCellPlatform sharedInstance] GetEveData] UTF8String];
}

/**
 *获取游戏的clientid
 */
const char * CPlatformInterface_IOS::GetClientId()
{
    return [[[FunCellPlatform sharedInstance] GetClientId] UTF8String];
}

/**
 *获取最新平台AccessToken
 */
const char * CPlatformInterface_IOS::GetAccessToken()
{
    return [[[FunCellPlatform sharedInstance] GetAccessToken] UTF8String];
}

const char* CPlatformInterface_IOS::CallFunction(const char * pluginType, const char * sdkType, const char * function, const char * values)
{
    NSString* ns_pluginType = [NSString stringWithUTF8String:pluginType];
    NSString* ns_sdkType = [NSString stringWithUTF8String:sdkType];
    NSString* ns_function = [NSString stringWithUTF8String:function];
    
    NSDictionary *dict = nil;
    if (values!=NULL && strcmp(values, "") !=0 && strcmp(values, "{}") !=0 ) {
        NSString* string = [NSString stringWithUTF8String:values];
        NSError *error=nil;
        NSData *jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
        dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        
        if (error)
        {
            NSLog(@"json解析失败：%@", error);
            return "{}";
        }
    }
    
    NSDictionary* dictReturn = [[FunCellPlatform sharedInstance] FCCallFunctionWithPlugintype:ns_pluginType sdkType:ns_sdkType function:ns_function values:dict];
    
    if (dictReturn == nil || [dictReturn isEqual:@{}]) {
        return "{}";
    }
    
    NSData *data =    [NSJSONSerialization dataWithJSONObject:dictReturn options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString* json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString* jsonString = [NSString stringWithString:json];
    [json release];
    json = nil;
    
    return [jsonString UTF8String];
}

