//
//  CartPaymentOptionsVC.swift
//  WOKA
//
//  Created by MacBook Pro on 23/07/24.
//

import UIKit

class CartPaymentOptionsVC: UIViewController {

    @IBOutlet weak var cartTableView: UITableView!
    @IBOutlet weak var cartTableHeight: NSLayoutConstraint!
    @IBOutlet weak var couponTableView: UITableView!
    @IBOutlet weak var couponTableHeght: NSLayoutConstraint!
    @IBOutlet weak var confirmBtn: LocalisedElementsButton!
    @IBOutlet weak var couponCodeTF: TextFieldShadow!
    @IBOutlet weak var totalCartPrice: LocalisedElementsLabel!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var applyBtn: UIButton!
    
    @IBOutlet weak var couponAplliedStack: UIStackView!
    @IBOutlet weak var subtotalPrice: LocalisedElementsLabel!
    @IBOutlet weak var discountPrice: LocalisedElementsLabel!
    @IBOutlet weak var noCouponFound: LocalisedElementsLabel!
    
    var vm = CartPaymentOptionsVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        vm.vc = self
        vm.initView()
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.setColor(color: .white)

    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: animated)
        if self.isMovingFromParent {
            // Back button was pressed
            PersistentStorage.shared.addOthersCount()
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        // Customize the navigation bar's appearance
        self.navigationController?.setColor(color: .black)
    }

    @IBAction func offerCouponBtnTapped(_ sender: UIButton) {
        vm.isExpanded.toggle()
        PersistentStorage.shared.addShopCount(postID: 0)
        if vm.isExpanded{
            sender.setImage(UIImage(systemName: "chevron.down.circle.fill"), for: .normal)
            self.couponTableView.isHidden = false
        }else{
            sender.setImage(UIImage(systemName: "chevron.forward.circle.fill"), for: .normal)
            self.couponTableView.isHidden = true
        }
    }
    
    @IBAction func confirmBtnTapped(_ sender: LocalisedElementsButton) {
        PersistentStorage.shared.addShopCount(postID: 0)
        let sb = UIStoryboard(name: K.StoryBoard.address, bundle: nil)
        let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Address.addressListVC) as! AddressListVC
        vcPush.vm.couponCodeApplied = vm.couponCodeApplied
        self.navigationController?.pushViewController(vcPush, animated: true)
//        vm.createOrder()
    }
    
    @IBAction func applyBtnTapped(_ sender: UIButton) {
        PersistentStorage.shared.addShopCount(postID: 0)
        if self.couponCodeTF.text == "" {
            Utilities.alertWithBtn(title: "", msgBody: "Please enter coupon code", okBtnStr: "OK", vc: self)
            return
        }
        vm.applyCoupon(code: self.couponCodeTF.text!)
    }
}
// MARK: - TableView DataSource , Delegates

extension CartPaymentOptionsVC : TableViewSRC{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == cartTableView{
            return CartDataCache.cartListData.count
        }else{
            return vm.couponData.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == cartTableView{
            let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.Cart.cartPaymentOptionsCell) as! CartPaymentOptionsCell
            let data = CartDataCache.cartListData[indexPath.row]
            cell.setData(data: data)
            return cell
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: K.CellIdentifier.Cart.couponCell) as! CouponCell
            let data = vm.couponData[indexPath.row]
            cell.setData(data: data, isSelected: vm.couponCodeSelected == data.couponCode ? true : false)
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView == couponTableView{
            return 60
        }else{
            return UITableView.automaticDimension
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == couponTableView{
            PersistentStorage.shared.addShopCount(postID: 0)
            let couponCode = vm.couponData[indexPath.row].couponCode
            vm.couponCodeSelected = vm.couponData[indexPath.row].couponCode ?? ""
            self.couponCodeTF.text = couponCode
            tableView.reloadData()
        }
    }
}

