HadoopDetailController.swift 7.1 KB
//
//  HadoopDetailController.swift
//  AoleiSports
//
//  Created by ilCode on 2024/6/25.
//

import UIKit
import DGCharts

class HadoopDetailController: BaseController {
    let yqId: Int
    var provider: HadoopProvider<HadoopTarget>?
    let activities = ["进攻", "排名", "技术", "状态", "防守"]
    
    private lazy var header: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: -30, width: kScreenW, height: kNavBarH + 90))
        view.backgroundColor = UIColor.colorWith(hexString: "122063")
        view.corners(radius: 30)
        return view
    }()
    
    private lazy var teamView: UIView = {
        let view = UIView(frame: CGRect(x: 10, y: kNavBarH, width: kScreenW - 20, height: 100))
        view.backgroundColor = UIColor.colorWith(hexString: "ECECED")
        view.corners(radius: 20)
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = view.bounds
        gradientLayer.colors = [UIColor.colorWith(hexString: "DADADA").cgColor, UIColor.colorWith(hexString: "F4F4F4").cgColor, UIColor.colorWith(hexString: "DADADA").cgColor]
        gradientLayer.locations = [0.0, 0.5, 1.0]
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
        view.layer.insertSublayer(gradientLayer, at: 0)
        return view
    }()
    
    private lazy var radarView: UIView = {
        let view = UIView()
        view.backgroundColor = kWhite
        view.corners(radius: 10)
        view.border(borderColor: UIColor.colorWith(hexString: "F2F1F1"))
        return view
    }()
    
    private lazy var radarChartView: RadarChartView = {
        let chartView = RadarChartView()
        chartView.chartDescription.enabled = false
        chartView.webLineWidth = 1
        chartView.innerWebLineWidth = 1
        chartView.webColor = UIColor.colorWith(hexString: "CADEF1")
        chartView.innerWebColor = UIColor.colorWith(hexString: "CADEF1")
        chartView.webAlpha = 1
        chartView.backgroundColor = .clear
        chartView.rotationEnabled = false
        return chartView
    }()
        
    init(yqId: Int) {
        self.yqId = yqId
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        gk_navTitle = "大数据研究"
        gk_navBackgroundColor = UIColor.colorWith(hexString: "122063")
        gk_navTitleColor = kWhite
        gk_backImage = R.image.btn_back_white()
        
//        view.addSubview(header)
//        view.addSubview(teamView)
//        view.addSubview(radarView)
//        radarView.addSubview(radarChartView)
//        
//        radarView.snp.makeConstraints { make in
//            make.left.equalToSuperview().offset(10)
//            make.right.equalToSuperview().offset(-10)
//            make.top.equalTo(teamView.snp.bottom).offset(10)
//        }
//        radarChartView.snp.makeConstraints { make in
//            make.centerX.equalToSuperview()
//            make.top.equalToSuperview().offset(10)
//            make.width.height.equalTo(300)
//            make.bottom.equalToSuperview().offset(-10)
//        }
//        
//        self.initRadarChartView()
//        self.showRadarChartView()
        
        initProvider()
        handleRefresh()
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func handleRefresh() {
        super.handleRefresh()
        
        provider?.bigDataDetailRequest(yqId: yqId, completion: { logicResult in
            if case .failure(let err) = logicResult {
                if case AsError.netErr(let message) = err {
                    self.showEmpty(type: .netErr, message: message)
                } else {
                    self.showEmpty(type: .other, message: err.localizedDescription)
                }
            }
        })
    }
    
    private func initRadarChartView() {
        let xAxis = radarChartView.xAxis
        xAxis.labelFont = kBoldFontSize(12)
        xAxis.xOffset = 0
        xAxis.yOffset = 0
        xAxis.valueFormatter = self
        xAxis.labelTextColor = UIColor.colorWith(hexString: "475086")
        
        let yAxis = radarChartView.yAxis
        yAxis.labelFont = kBoldFontSize(12)
        yAxis.axisMinimum = 0
        yAxis.drawLabelsEnabled = false
        yAxis.labelTextColor = UIColor.colorWith(hexString: "475086")
        
        let l = radarChartView.legend
        l.form = .circle
        l.horizontalAlignment = .center
        l.verticalAlignment = .bottom
        l.orientation = .horizontal
        l.drawInside = true
        l.font = kFontSize(12)
        l.xEntrySpace = 10
        l.textColor = kMainTitleColor
    }
    
    func showRadarChartView() {
        let hostList = [44, 85, 71, 86, 80]
        let guestList = [17, 91, 45, 83, 77]
        let axisRanges: [(min: Double, max: Double)] = [
            (min: 0, max: 54),
            (min: 0, max: 100),
            (min: 0, max: 100),
            (min: 0, max: 100),
            (min: 0, max: 100)
        ]
        
        // 归一化数据
        let normalizedHostList = zip(hostList, axisRanges).map { value, range in
            return RadarChartDataEntry(value: (Double(value) - range.min) / (range.max - range.min))
        }
        let normalizedGuestList = zip(guestList, axisRanges).map { value, range in
            return RadarChartDataEntry(value: (Double(value) - range.min) / (range.max - range.min))
        }
        
        let set1 = RadarChartDataSet(entries: normalizedHostList, label: "主队")
        set1.setColor(UIColor.colorWith(hexString: "1EA1FC"))
        set1.fillColor = UIColor.colorWith(hexString: "73C4FD")
        set1.drawFilledEnabled = true
        set1.fillAlpha = 0.7
        set1.lineWidth = 2
        set1.drawHighlightCircleEnabled = true
        set1.setDrawHighlightIndicators(false)
        
        let set2 = RadarChartDataSet(entries: normalizedGuestList, label: "客队")
        set2.setColor(UIColor.colorWith(hexString: "FC9377"))
        set2.fillColor = UIColor.colorWith(hexString: "FBBAAA")
        set2.drawFilledEnabled = true
        set2.fillAlpha = 0.7
        set2.lineWidth = 2
        set2.drawHighlightCircleEnabled = true
        set2.setDrawHighlightIndicators(false)
        
        let data: RadarChartData = [set1, set2]
        data.setDrawValues(false)
        radarChartView.data = data
        
        radarChartView.animate(xAxisDuration: 0, yAxisDuration: 1.4, easingOption: .linear)
    }
}

extension HadoopDetailController: AxisValueFormatter {
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return activities[Int(value) % activities.count]
    }
}

extension HadoopDetailController {
    private func initProvider() {
        provider = HadoopProvider(vc: self, showErr: false)
        
//        provider?.matchList
//            .asObservable()
//            .subscribe(onNext: { [weak self] tmpModels in
//                guard let self = self else { return }
//                
//                
//            })
//            .disposed(by: disposeBag)
    }
}