//
//  XVMainJobViewController.m
//  fastservice
//
//  Created by xuning on 2/4/24.
//  Copyright © 2024 FastService. All rights reserved.
//

#import "XVMainJobViewController.h"
#import "XVCustomBarView.h"
#import "XVCategoryTitleView.h"
#import "XVNewJobViewController.h"
#import "XVOrderSortView.h"
#import "XVPersonalViewController.h"
#import <TXScrollLabelView.h>
#import "WMDragView.h"
#import "XVOrderSearchView.h"

@interface XVMainJobViewController ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate>

@property (nonatomic, strong) XVCustomBarView *barView;

@property (nonatomic, strong) XVCategoryTitleView *categoryView;

@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;

@property (nonatomic, strong) XVOrderSortView *sortView;

@property (nonatomic,assign) NSInteger currentIndex;

@property(nonatomic, strong) TXScrollLabelView *marqueeView;

@property(nonatomic,strong) WMDragView *dragView;

@property (nonatomic, strong) XVOrderSearchView *searchView;


@end

@implementation XVMainJobViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI {
    [self.view addSubview:self.barView];
    [self.view addSubview:self.categoryView];
    
    [self.view addSubview:self.marqueeView];
    
    [self.view addSubview:self.listContainerView];
    [self.view addSubview:self.sortView];

    CGFloat h = (SCREEN_HEIGHT - self.listContainerView.tx_y) / 2;
    
    self.dragView = [[WMDragView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-48, h, 48, 48)];

    self.dragView.imageView.image = IMG(@"sosuo-1");
    kViewBorderRadius(self.dragView, 24, 0, [UIColor whiteColor]);
    
    [self.view addSubview:self.dragView];
    self.dragView.isKeepBounds = YES;
    self.dragView.freeRect = CGRectMake(SCREEN_WIDTH/2, self.listContainerView.top, SCREEN_WIDTH/2, h-100);
    self.dragView.keepDirection = WMKeepDirectionInitialize;
    
    WS(weakSelf)
    self.dragView.clickDragViewBlock = ^(WMDragView *dragView) {

        weakSelf.searchView.top = dragView.top;
        
        [UIView animateWithDuration:0.8 animations:^{
            dragView.hidden = YES;
            [weakSelf.searchView show];
        }];
        
    };
    
    
    [self.view addSubview:self.searchView];

}

// 点击选中或者滚动选中都会调用该方法。适用于只关心选中事件，不关心具体是点击还是滚动选中的。
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    
}

// 点击选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    
    if (self.currentIndex == 0 && index == 0 && categoryView.selectedIndex == 0) {

        if(self.sortView.hidden) {
        
            
            self.sortView.hidden = NO;
        }else{
            
            self.sortView.hidden = YES;
        }
    }else{
        self.sortView.hidden = YES;
    }
    if(index == 4) {
        // 路线规划
    }
    
    self.currentIndex = index;
    
    
    
}

// 滚动选中的情况才会调用该方法
- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    
}

// 正在滚动中的回调
- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    
}
// 返回列表的数量
- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.categoryView.titles.count;
}
// 根据下标 index 返回对应遵守并实现 `JXCategoryListContentViewDelegate` 协议的列表实例
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    return [[XVNewJobViewController alloc] init];
}


- (BOOL)fd_prefersNavigationBarHidden {
    return YES;
}
- (void)textFieldDidChange:(UITextField *)textField {
    
}

- (XVCustomBarView *)barView {
    if(_barView == nil) {
        _barView = [[XVCustomBarView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, KSTATUSBAR_HEIGHT + 45)];
        _barView.backgroundColor = [UIColor whiteColor];
        WS(weakSelf);
        [_barView.centerButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            XVPersonalViewController *vc = [[XVPersonalViewController alloc]init];
            CWLateralSlideConfiguration *config =  [CWLateralSlideConfiguration configurationWithDistance:kCWSCREENWIDTH * 0.8 maskAlpha:0.4 scaleY:1.0 direction:CWDrawerTransitionFromLeft backImage:nil];
            [weakSelf cw_showDrawerViewController:vc animationType:CWDrawerAnimationTypeMask configuration:config];

        }];
    }
    return _barView;
}

- (XVCategoryTitleView *)categoryView {
    if(_categoryView == nil) {
        _categoryView = [[XVCategoryTitleView alloc] initWithFrame:CGRectMake(0, self.barView.height, SCREEN_WIDTH, 50)];
        _categoryView.titleColor = F_415058_COLOR;
        _categoryView.titleSelectedColor = COLOR_THEMEGRENN;
        _categoryView.titleFont = [UIFont systemFontOfSize:14];
        _categoryView.titleSelectedFont = [UIFont systemFontOfSize:14];
        _categoryView.backgroundColor = [UIColor whiteColor];
        _categoryView.delegate = self;
        
        
        _categoryView.titles = @[@"新任务", @"待取货", @"配送中",@"今日完成",@"已取消"];
        
        
        _categoryView.uiTypes = @{@(0) : @(XVCategoryTitleUIType_SingleImage),
                                  @(1) : @(XVCategoryTitleUIType_SingleImage),
                                  @(2) : @(XVCategoryTitleUIType_SingleImage),
                                  @(3) : @(XVCategoryTitleUIType_None),
                                  @(4) : @(XVCategoryTitleUIType_None)}.mutableCopy;
        _categoryView.imageLocations = @{@(0) : @(XVCategoryTitleImageLocation_Right),
                                         @(1) : @(XVCategoryTitleImageLocation_Right),
                                         @(2) : @(XVCategoryTitleImageLocation_Right),
                                         @(4) : @(XVCategoryTitleImageLocation_Left)}.mutableCopy;
        _categoryView.singleImages = @{@(0) : [UIImage imageNamed:@"fas fa-sort"],
                                       @(1) : [UIImage imageNamed:@"fas fa-sort"],
                                       @(2) : [UIImage imageNamed:@"fas fa-sort"],}.mutableCopy;
        
        
        JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
        lineView.indicatorColor = COLOR_THEMEGRENN;
        lineView.verticalMargin = 6;
        lineView.indicatorWidth = JXCategoryViewAutomaticDimension;
        _categoryView.indicators = @[lineView];
        _categoryView.listContainer = self.listContainerView;
    }
    return _categoryView;
}

- (TXScrollLabelView *)marqueeView {
    if (_marqueeView == nil) {
        _marqueeView = [TXScrollLabelView scrollWithTitle:@"测试跑马灯" type:0];
        _marqueeView.font = [UIFont systemFontOfSize:12];
        _marqueeView.scrollSpace = 100;
        _marqueeView.backgroundColor = kHexRGB(0xDBDBDB);
        _marqueeView.scrollTitleColor = [UIColor whiteColor];
        _marqueeView.frame = CGRectMake(10, self.barView.height + self.categoryView.height, SCREEN_WIDTH-20, 31);
        kViewBorderRadius(_marqueeView, 8, 0, kHexRGB(0xDBDBDB));
    }
    return _marqueeView;
}

- (JXCategoryListContainerView *)listContainerView {
    if(_listContainerView == nil) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
        
        CGFloat h = self.barView.height + self.categoryView.height + self.marqueeView.height + 6;
        _listContainerView.frame = CGRectMake(0, h, SCREEN_WIDTH, SCREEN_HEIGHT - h);
        _listContainerView.scrollView.scrollEnabled = NO;
    }
    return _listContainerView;
}

- (XVOrderSortView *)sortView {
    if(_sortView == nil) {
        _sortView = [[XVOrderSortView alloc]initWithFrame:CGRectMake(0, self.barView.height + self.categoryView.height, SCREEN_WIDTH, SCREEN_HEIGHT)];
        
        _sortView.hidden = YES;
    }
    return _sortView;
}

- (XVOrderSearchView *)searchView {
    if (_searchView == nil) {
        _searchView = [[XVOrderSearchView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, self.dragView.top, 311, 48)];
        WS(weakSelf);
        _searchView.cancelBlock = ^{
            weakSelf.dragView.hidden = NO;
        };
    }
    return _searchView;
}

@end
