//
//  PriceSetting.m
//  iSales-NPD
//
//  Created by Jack on 2016/10/11.
//  Copyright © 2016年 United Software Applications, Inc. All rights reserved.
//

#import "PriceSettingViewController.h"
#import "const.h"
#import "EnumSelectViewController.h"
#import "CategoryPriceViewController.h"
#import "iSalesNetwork.h"
#import "Singleton.h"
#import <objc/runtime.h>
#import "MainViewController.h"
#import "NotificationNameCenter.h"




@interface PriceSettingViewController ()

@property (strong, nonatomic) IBOutlet UIButton *priceTypeButton;

@property (strong, nonatomic) IBOutlet UIButton *showButton;
@property (strong, nonatomic) IBOutlet UIButton *setPriceButton;

@end

@implementation PriceSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.priceType = [Singleton sharedInstance].npd_shop_price_type;
    
    [self configAppearance];
    
}

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

#pragma mark - Customer Inint

- (void)configAppearance {
    

    self.setPriceButton.hidden = ![Singleton sharedInstance].permissions_price_setting;

    
    UIApplication * app = [UIApplication sharedApplication];
    AppDelegate *appDelegate = (AppDelegate *)[app delegate];
    
    self.showPrice = !appDelegate.price_hidden;
    
    [self changePriceType:self.priceType];
    self.showButton.selected = !self.showPrice;
    
    UIBarButtonItem *closeButton =   [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                      style:UIBarButtonItemStylePlain
                                                                     target:self
                                                                     action:@selector(onCloseClick:)];
    self.navigationItem.rightBarButtonItem = closeButton;
    
}

- (void)changePriceType:(NSInteger)type {
    
    NSString *title = [Singleton sharedInstance].deliveryString;
    switch (type) {
        case 0: {
            title = [Singleton sharedInstance].deliveryString;
        }
            break;
        case 1: {
            title = flat_price;
        }
            break;
        case 2: {
            title = given_price;
        }
            break;
            
        default:
            break;
    }
    
    self.priceType = type;
    [self.priceTypeButton setTitle:title forState:UIControlStateNormal];
    
}

#pragma mark - action

- (void)onCloseClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (IBAction)showOrHidePriceButtonClick:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    self.showPrice = !sender.selected;
    
    UIApplication * app = [UIApplication sharedApplication];
    AppDelegate *appDelegate = (AppDelegate *)[app delegate];
    
    [appDelegate set_priceHidden:sender.selected];
    
//    objc_msgSend(appDelegate.main_vc,@selector(initMenuItems));// initMenuItems 不是公开的方法
    [(MainViewController *)appDelegate.main_vc initMenuItems];
}

- (IBAction)calculatePriceButtonClick:(UIButton *)sender {

    UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Load Data"];
    
    NSDictionary *dic = [iSalesNetwork get_npd_shop_givenPrice];

    [waitalert dismissWithClickedButtonIndex:0 animated:YES];
    
    if ([[dic objectForKey:@"result"] integerValue] == 2) {
    
        CategoryPriceViewController *categoryPriceVC =[ [UIStoryboard storyboardWithName:@"CUL" bundle:nil] instantiateViewControllerWithIdentifier:@"CategoryPriceViewController"];
        
        categoryPriceVC.categoryGivenPrice = dic.mutableCopy;
        
        [self.navigationController pushViewController:categoryPriceVC animated:YES];
    }
}

- (NSString *)checkPriceType:(NSInteger)priceType {
    return  self.priceType == priceType ? @"1" : @"0";
}

- (NSInteger)checkedCadedate:(NSDictionary *)cadedate {
    
    int priceType = 0;
    int count = [[cadedate objectForKey:@"count"] intValue];
    for (int i = 0; i < count; i++) {
        NSDictionary *dic = [cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]];
        int check = [[dic objectForKey:@"check"] intValue];
        if (check) {
            
            priceType = [[dic objectForKey:@"value_code"] integerValue];
            
            break;
        }
    }
    
    return priceType;
}

- (IBAction)priceTypeButtonClick:(UIButton *)sender {
    
    EnumSelectViewController* enumvc =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"EnumSelectorViewController"];

    enumvc.max_select = 1;
    
    
//    enumvc.cadedate = @{
//                        @"count" : @"3",
//                        @"val_0" : @{@"value" : [Singleton sharedInstance].deliveryString,@"value_code" : @"0",@"check" : [self checkPriceType:0]},
//                        @"val_1" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]},
//                        @"val_2" : @{@"value" : given_price,@"value_code" : @"2",@"check" : [self checkPriceType:2]}
//                        }.mutableCopy;
    
    enumvc.cadedate = @{
                        @"count" : @"2",
                        @"val_0" : @{@"value" : flat_price,@"value_code" : @"1",@"check" : [self checkPriceType:1]},
                        @"val_1" : @{@"value" : given_price,@"value_code" : @"2",@"check" : [self checkPriceType:2]}
                        }.mutableCopy;
    
    enumvc.title = @"";
    
    
    enumvc.single_select =true;
    
    __weak typeof(self) weakSelf = self;
    
    enumvc.returnValue = ^(NSMutableDictionary* value){
        
        if (weakSelf) {
            
            __strong typeof(weakSelf) strongSelf = weakSelf;
            
            [strongSelf changePriceType:[strongSelf checkedCadedate:value]];

        }
        
    };
    
    [self.navigationController pushViewController:enumvc animated:true];
}

- (IBAction)saveButtonClick:(UIButton *)sender {
    
    UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Set PriceType"];
    
    NSDictionary *dic = [iSalesNetwork set_npd_shop_price_type:self.priceType];
    
    [waitalert dismissWithClickedButtonIndex:0 animated:YES];
    // 成功
    if ([[dic objectForKey:@"result"] integerValue] == 2) {
        
        [Singleton sharedInstance].npd_shop_price_type = self.priceType;
    
        // 刷新 Cart、Order List
        [[NSNotificationCenter defaultCenter] postNotificationName:Change_Price_Type_Notification object:nil];
        
    } else { // 失败
        
        DebugLog(@"set price type failure");
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
