//
//  SettingsVC.swift
//  Garmentoo
//
//  Created by Vijith on 30/03/22.
//

import UIKit

struct settingsData
{
    var action = [Int]()
    var title = String()
    var sectionData = [String]()
    var image = [String]()
}

class SettingsVC: UIViewController {
    @IBOutlet weak var settingsTable: UITableView!
    
   var settings  = [settingsData]()
    
//    var settingsTitle = ["Notification","Change Address","My Purchases","My Wallet","Logout"]
//    var settingsIcons = [UIImage(named: "notification"),UIImage(named: "location"),UIImage(named: "purchase"),UIImage(named: "wallet"),UIImage(named: "logout"),]
    
    var isNotification = false
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        settings = [settingsData(action:[0,1] , title: "Account", sectionData: ["View Profile","My Orders"],image: ["profile","purchase"]),
        settingsData(action: [2], title: "Notifications", sectionData: ["Notification"], image: ["notification"]),
        settingsData(action: [1,1], title: "More", sectionData: ["Address Management","Privacy Policy"], image: ["location","privacy"])
        ]
    }
    
    @objc func notifBtnTap(sender: UIButton){
        isNotification = !isNotification
        DispatchQueue.main.async {
            self.settingsTable.reloadData()
        }
       
    }
    /*
    func moveToVC(section:Int,index:Int)
    {
        switch section {
        case 0:
            switch index {
            case 0:
                //go to profile page
            default:
                //go to orders page
            }
        default:
            <#code#>
        }
    }
 */
    
}

extension SettingsVC:UITableViewDelegate,UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        settings.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settings[section].sectionData.count + 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.row == 0)
        {
            guard  let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell", for: indexPath) as? SettingsHeaderTableViewCell
            else {
                return UITableViewCell()
            }
            cell.lbl.text = settings[indexPath.section].title
            return cell
        }
        else
        {
            guard  let cell = tableView.dequeueReusableCell(withIdentifier: "settingsCell", for: indexPath) as? SettingsTableViewCell
            else {
                return UITableViewCell()
            }
            cell.lbl.text = settings[indexPath.section].sectionData[indexPath.row - 1]
            
            cell.img.image = UIImage(named: settings[indexPath.section].image[indexPath.row - 1])
            
            if isNotification {
                cell.notifBtn.setImage(UIImage(named: "able"), for: .normal)
            }
            else
            {
                cell.notifBtn.setImage(UIImage(named: "disable"), for: .normal)
            }
            
            // to set image according to each type
            switch (settings[indexPath.section].action[indexPath.row - 1]) {
            case 2:
                cell.downImg.isHidden = false
               // cell.notifBtn.isHidden = false
                cell.notifBtn.isHidden = true
            case 1:
                cell.downImg.isHidden = false
                cell.notifBtn.isHidden = true
            
            default:
                cell.downImg.isHidden = false
                cell.notifBtn.isHidden = true
            }
            cell.notifBtn.addTarget(self, action: #selector(notifBtnTap(sender:)), for: .touchUpInside)
            
            return cell
        }
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if !(indexPath.row == 0)
        {
        switch indexPath.section {
        case 0:
            switch indexPath.row {
            case 1:
                print("profile")
                let sb = UIStoryboard(name: "Main", bundle: nil)
                let nextVc = sb.instantiateViewController(withIdentifier: "ProfileVC")as! ProfileVC
                nextVc.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(nextVc, animated: true)
            default:
                let sb = UIStoryboard(name: "Main", bundle: nil)
                let nextVc = sb.instantiateViewController(withIdentifier: "MyOrdersVC")as! MyOrdersVC
                nextVc.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(nextVc, animated: true)
            }
            
        case 1 :
            let sb = UIStoryboard(name: "Main", bundle: nil)
            let nextVc = sb.instantiateViewController(withIdentifier: "NotificationVC")as! NotificationVC
            nextVc.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(nextVc, animated: true)
        default:
            switch (indexPath.row) {
            case 1:
                let sb = UIStoryboard(name: "Main", bundle: nil)
                let nextVc = sb.instantiateViewController(withIdentifier: "MyAddressVC")as! MyAddressVC
                nextVc.isFromCart = false
                nextVc.hidesBottomBarWhenPushed = true
                self.navigationController?.pushViewController(nextVc, animated: true)
            default:
                print("123")
            }
        }

    }
    
    }
    
    
}
/*
extension SettingsVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settingsTitle.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "settingsCell", for: indexPath) as! SettingsTableViewCell
        cell.lbl.text = settingsTitle[indexPath.row]
        
        cell.img.image = settingsIcons[indexPath.row]
        
        if isNotification {
            cell.notifBtn.setImage(UIImage(named: "radio-button"), for: .normal)
        }
        else
        {
            cell.notifBtn.setImage(UIImage(named: "radio-button-2"), for: .normal)
        }
        
        // to set image according to each type
        switch indexPath.row {
        case 0:
            cell.downImg.isHidden = true
            cell.notifBtn.isHidden = false
        case 1,2,3:
            cell.downImg.isHidden = false
            cell.notifBtn.isHidden = true
        
        default:
            cell.downImg.isHidden = true
            cell.notifBtn.isHidden = true
        }
        cell.notifBtn.addTarget(self, action: #selector(notifBtnTap(sender:)), for: .touchUpInside)
        
        return cell
    }
    
    @objc func notifBtnTap(sender: UIButton){
        isNotification = !isNotification
        DispatchQueue.main.async {
            self.settingsTable.reloadData()
        }
       
    }
    
}
*/

// for hiding tabbar when scrolls
extension SettingsVC:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
            //scrolling down
            //self.scrollBottomConstraint.constant = 0
            changeTabBar(hidden: true, animated: true)
        }
        else{
            //scrolling up
           // self.scrollBottomConstraint.constant = 20
            changeTabBar(hidden: false, animated: true)
        }
    }
    
    func changeTabBar(hidden:Bool, animated: Bool){
           let tabBar = self.tabBarController?.tabBar
           let offset = (hidden ? UIScreen.main.bounds.size.height : UIScreen.main.bounds.size.height - (tabBar?.frame.size.height)! )
           if offset == tabBar?.frame.origin.y {return}
           print("changing origin y position")
           let duration:TimeInterval = (animated ? 0.5 : 0.0)
           UIView.animate(withDuration: duration,
                          animations: {tabBar!.frame.origin.y = offset},
                          completion:nil)
       }
}
