//
//  ALPlatformView.m
//  GMAlpha
//
//  Created by Mikasa on 2019/1/10.
//  Copyright © 2019 Gengmei. All rights reserved.
//

#import "ALPlatformView.h"
#import "ALShareButton.h"
#import "GMShareSDK.h"
#import <GMKit/GMLabel.h>
#import <GMKit/UIColor+GMTheme.h>
#import <GMFoundation/NSString+GM.h>
#import <Masonry/Masonry.h>
#import <GMKit/GMButton.h>
#import <GMKit/Constant.h>
#import <MBProgressHUD/MBProgressHUD.h>
#import <GMThirdPartyDefine.h>


@interface ALPlatformView()
@property (nonatomic, strong) NSArray *platformArray;
@end
@implementation ALPlatformView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self addDefaultPlatform];
    }
    return self;
}

// 添加
- (void)addDefaultPlatform {
    
    self.platformArray = @[@(GMSharePlatformWechatSession),
                           @(GMSharePlatformWechatTimeline),
                           @(GMSharePlatformQQFriend),
                           @(GMSharePlatformSinaWeibo)];
    [self addPlatformTypeSubView];
}

#pragma mark - addSinInTypeSubView
- (void)addPlatformTypeSubView {
    
    for (NSNumber *type in self.platformArray) {
        [self addSinInType:[type integerValue]];
    }
    
    NSInteger count = self.subviews.count;
    CGFloat space = (MAINSCREEN_WIDTH - (self.subviews.count * kALShareButtonW))/(self.subviews.count +1);
    __weak __typeof(self)weakSelf = self;
    for (int i = 0; i < count; i++) {
        ALShareButton *button = self.subviews[i];
        if (i == 0) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(weakSelf.mas_centerY);
                make.left.mas_equalTo(space);
                make.size.mas_equalTo(CGSizeMake(kALShareButtonW, kALShareButtonH));
            }];
        } else {
            ALShareButton *prebutton = self.subviews[i-1];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(weakSelf.mas_centerY);
                make.size.mas_equalTo(CGSizeMake(kALShareButtonW, kALShareButtonH));
                make.left.mas_equalTo(prebutton.mas_right).offset(space);
            }];
        }
    }
}

- (void)addSinInType:(GMSharePlatform)type {
    
    NSString *iconString = @"";
    NSString *title = @"";
    switch (type) {
        case GMSharePlatformWechatSession:
        {
            iconString = @"share_weixin_session";
            title = @"微信";
        }
            break;
        case GMSharePlatformSinaWeibo:
        {
            iconString = @"share_sina_weibo";
            title = @"微博";
        }
            break;
        case GMSharePlatformWechatTimeline:
        {
            iconString = @"share_weixin_timeline";
            title = @"朋友圈";
        }
            break;
        case GMSharePlatformQQFriend:
        {
            iconString = @"share_qq";
            title = @"QQ";
        }
            break;
        default:
            break;
    }
    ALShareButton *button = [[ALShareButton alloc] init];
    button.tag = 1000 + type;
    [button setImage:[UIImage imageNamed:iconString] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:iconString] forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(sinInTypeClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

// 其他平台
- (void)sinInTypeClick:(GMButton *)sender {
    
    GMSharePlatform platform = sender.tag - 1000;
    
    if (![GMShareSDK isClientInstalled:platform] &&
        (platform == GMSharePlatformWechatTimeline | platform == GMSharePlatformWechatSession)) {
        //QQ和新浪支持web分享，只有微信不支持，这里优化下提示语
        [self toast:@"您未安装微信客户端"];
        return;
    }
    
    if (self.sharePlatformClick) {
        self.sharePlatformClick(platform);
    }
    
}

/** *  @brief  分享结果提示*/
- (void)toast:(NSString *)text{
    
//    [_hud hide:YES];
    MBProgressHUD *hudProgrss = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hudProgrss.mode = MBProgressHUDModeText;
    hudProgrss.userInteractionEnabled = NO;
    hudProgrss.labelText = text;
    hudProgrss.removeFromSuperViewOnHide = YES;
    [hudProgrss hide:YES afterDelay:1.0];
}
@end
