//
//  ICIAPViewController.m
//  superCleaner
//
//  Created by Mazy on 2020/7/5.
//  Copyright © 2020 Mazy. All rights reserved.
//

#import "ICIAPViewController.h"
#import "superCleaner-Swift.h"
#import "ICWebViewController.h"
#import "ICIPAProductModel.h"

@interface ICIAPViewController ()

@property (weak, nonatomic) IBOutlet UIView *weakContentView;
@property (weak, nonatomic) IBOutlet UIView *yearContentView;

@property (weak, nonatomic) IBOutlet UILabel *weakPeriodOfUseLabel;
@property (weak, nonatomic) IBOutlet UILabel *yearPeriodOfUseLabel;

@property (weak, nonatomic) IBOutlet UILabel *weakPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *yearPriceLabel;

@property (weak, nonatomic) IBOutlet UILabel *discountLabel;

@property (weak, nonatomic) IBOutlet UILabel *funcTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *function1Label;
@property (weak, nonatomic) IBOutlet UILabel *function2Label;
@property (weak, nonatomic) IBOutlet UILabel *function3Label;

@property (nonatomic, strong) NSMutableArray *products;
@property (nonatomic, strong) ICIPAProductModel *currentProduct;

@end

@implementation ICIAPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.weakContentView.layer.cornerRadius = KScaleWidth(8);
    self.yearContentView.layer.cornerRadius = KScaleWidth(8);;

    UITapGestureRecognizer *weakTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedWeakAction)];
    [self.weakContentView addGestureRecognizer: weakTap];
    
    UITapGestureRecognizer *yearTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectedYearAction)];
    [self.yearContentView addGestureRecognizer: yearTap];
    
    self.products = [NSMutableArray array];
    [self getIpaList];
        
    self.funcTitleLabel.text = ICLocalized(@"AllFunctions"); //@"无限制\n访问所有功能";
    self.function1Label.text = ICLocalized(@"RemoveUnwantFile");//@"超快去除无用文件";
    self.function2Label.text = ICLocalized(@"SecurelyData");//@"安全隐藏敏感数据";
    self.function3Label.text = ICLocalized(@"ForgetAdvert");//@"忘掉广告和限制";
}

-(void)getIpaList {
    __weak typeof(self) weakSelf = self;
    [[ZTNetworkTools shared] getWithAction:getIPAPayConfig parameters:@{} success:^(id  _Nonnull response) {
        if ([[response objectForKey:@"status"] intValue] == 200) {
            NSArray *goodList = response[@"result"][@"data"][@"goodsList"];
            
            for (NSDictionary *good in goodList) {
                ICIPAProductModel *model = [ICIPAProductModel modelWithDic:good];
                if (model != nil) {
                    [weakSelf.products addObject:model];
                    if ([model.productId containsString:@"week"]) {
                        weakSelf.weakPeriodOfUseLabel.text = model.name;
                        weakSelf.weakPriceLabel.text = model.payDesc;
                    } else if ([model.productId containsString:@"year"]) {
                        weakSelf.yearPeriodOfUseLabel.text = model.name;
                        weakSelf.yearPriceLabel.text = [NSString stringWithFormat:@"%@ (%@)", model.payDesc, model.greyBgStr];
                    }
                }
            }
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)selectedWeakAction {
    [MobClick event:@"pay_weekly"];
    if (self.products.count == 0){
        return;
    }
    for (ICIPAProductModel *model in self.products) {
        if ([model.productId containsString:@"week"]) {
            _currentProduct = model;
            break;
        }
    }
    [self startPlanAction:_currentProduct];
}

- (void)selectedYearAction {
    [MobClick event:@"pay_yearly"];
    if (self.products.count == 0){
        return;
    }
    for (ICIPAProductModel *model in self.products) {
        if ([model.productId containsString:@"year"]) {
            _currentProduct = model;
            break;
        }
    }
    [self startPlanAction:_currentProduct];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:true animated:true];
}

// dismiss
- (IBAction)dismissAction {
    [MobClick event:@"close_trial_page"];
    [self dismissViewControllerAnimated:true completion:nil];
}

// 恢复购买
- (IBAction)restoreAction {
    [MobClick event:@"resume_pay"];
    [ICPurchaseManager restorePurchasesWithSuccess:^{
        [[ICUserManager shared] getUserInfo:^(ZTUserInfoModel * _Nonnull model) {
            [self dismissViewControllerAnimated:true completion:nil];
        } failure:^{
            [self dismissViewControllerAnimated:true completion:nil];
        }];
    } toView:self.view];
}

// 开始计划
- (IBAction)startPlanAction:(ICIPAProductModel *)model {
    [MobClick event:@"description_pay_button"];
    [MobClick event:@"trial_use"];
    
    NSString *payTypeStr = model.productId;
    __weak typeof(self) weakSelf = self;
    [ICPurchaseManager purchaseWithProductIdWithProductId:payTypeStr inView:self.view success:^{
        [MobClick event:@"pay_suc"];
        [[ICUserManager shared] getUserInfo:^(ZTUserInfoModel * _Nonnull model) {
            [weakSelf dismissViewControllerAnimated:true completion:nil];
        } failure:^{
            [weakSelf dismissViewControllerAnimated:true completion:nil];
        }];
    } faliure:^{

    }];
}

// 使用条款
- (IBAction)showUseTermsAction {
    ICWebViewController *webView = [[ICWebViewController alloc] init];
    NSDictionary * dic = [ICUserManager shared].h5_url;
    webView.url = [dic objectForKey:@"user"];
    webView.webTitle = ICLocalized(@"TermsUse");
    [self.navigationController pushViewController:webView animated:true];
}

// 隐私政策
- (IBAction)showPrivacyPlicyAction {
    ICWebViewController *webView = [[ICWebViewController alloc] init];
    NSDictionary * dic = [ICUserManager shared].h5_url;
    webView.url = [dic objectForKey:@"privacy"];
    webView.webTitle = ICLocalized(@"PrivacyPolicy");
    [self.navigationController pushViewController:webView animated:true];
}

- (void)dealloc{
    NSLog(@"ICIAPViewController dealloc");
}

@end
