//
//  AV_TemperatureVC.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/7/9.
//

import UIKit

class AV_TemperatureVC: AV_RootVC {
    
    @IBOutlet weak var tableView: UITableView!
    var headarr = ["Heat","Warmandwarm","Comfortable","Lactation","Clod"]
    var namearr = ["Heat","Warm and warm","Comfortable","Lactation","Clod"]
    
    var selectedIndex: IndexPath?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setTableViewcell()
        
    }
    
    func setTableViewcell(){
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.register(UINib(nibName: "AV_SelectGenderCell", bundle: nil), forCellReuseIdentifier: "AV_SelectGenderCell")
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = true
        
    }
    
    @IBAction func back(_ sender: Any) {
        navigationController?.popViewController(animated: true)
    }
    
    
    @IBAction func skip(_ sender: Any) {
        let defaults = UserDefaults.standard
        defaults.set(2000, forKey: "waterml")
        let appDelegate = UIApplication.shared.delegate
        appDelegate?.window??.rootViewController = AV_NAVVC(rootViewController: AV_WaterHomeVC())

    }
    
    @IBAction func gohome(_ sender: Any) {
        let appDelegate = UIApplication.shared.delegate
        appDelegate?.window??.rootViewController = AV_NAVVC(rootViewController: AV_WaterHomeVC())
        
    }
    
}
extension AV_TemperatureVC:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 5
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "AV_SelectGenderCell", for: indexPath) as! AV_SelectGenderCell
        
        cell.headImagV.image = UIImage(named: self.headarr[indexPath.row])
        cell.nameLabel.text = self.namearr[indexPath.row]
        
        if indexPath == selectedIndex {
            
            cell.animateGradient(to: [UIColor.hexSting(color: "#69CCFB", alpha: 1).cgColor, //
                                      UIColor.hexSting(color: "#69CCFB", alpha: 1).cgColor])
        } else {
            cell.animateGradient(to: [UIColor.hexSting(color: "#F7F8F9", alpha: 1).cgColor, UIColor.hexSting(color: "F7F8F9", alpha: 1).cgColor])
        }
        
        
        
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          
        let previousSelectedIndex = selectedIndex
        
        // 更新选定的索引
        if selectedIndex == indexPath {
            selectedIndex = nil // 如果再次点击同一行则取消选择
        } else {
            selectedIndex = indexPath
        }
        
        //  重新加载先前选定的单元格和当前选定的单元格
        var indexPathsToReload: [IndexPath] = [indexPath]
        if let previous = previousSelectedIndex {
            indexPathsToReload.append(previous)
        }
        
        tableView.reloadRows(at: indexPathsToReload, with: .automatic)
        
        if indexPath.row == 0{
            let defaults = UserDefaults.standard
            defaults.set("Heat", forKey: "temperature")
        }else if indexPath.row == 1{
            let defaults = UserDefaults.standard
            defaults.set("Warm and warm exercise", forKey: "temperature")
        }else if indexPath.row == 2{
            let defaults = UserDefaults.standard
            defaults.set("Comfortable", forKey: "temperature")
        }else if indexPath.row == 3{
            let defaults = UserDefaults.standard
            defaults.set("Clod", forKey: "temperature")
        }
        
    }
    
}


