use std::ops::{Add, Sub};

use candid::CandidType;
use serde::{Deserialize, Serialize};

use crate::{AssetAmount, CandidAssetAmount};

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub enum BalanceChanged {
    Increase(AssetAmount),
    Decrease(AssetAmount),
}

#[derive(Debug, Clone, Deserialize, CandidType)]
pub enum CandidBalanceChanged {
    Increase(CandidAssetAmount),
    Decrease(CandidAssetAmount),
}

impl BalanceChanged {
    pub fn merge(&self, other: &Self) -> Self {
        match (self, other) {
            (BalanceChanged::Increase(a), BalanceChanged::Increase(b)) => {
                BalanceChanged::Increase(a.add(b))
            }
            (BalanceChanged::Decrease(a), BalanceChanged::Decrease(b)) => {
                BalanceChanged::Decrease(a.add(b))
            }
            (BalanceChanged::Increase(a), BalanceChanged::Decrease(b)) => {
                if a >= b {
                    BalanceChanged::Increase(a.sub(b))
                } else {
                    BalanceChanged::Decrease(b.sub(a))
                }
            }
            (BalanceChanged::Decrease(a), BalanceChanged::Increase(b)) => {
                if a >= b {
                    BalanceChanged::Decrease(a.sub(b))
                } else {
                    BalanceChanged::Increase(b.sub(a))
                }
            }
        }
    }
}

impl From<CandidBalanceChanged> for BalanceChanged {
    fn from(candid_balance_changed: CandidBalanceChanged) -> Self {
        match candid_balance_changed {
            CandidBalanceChanged::Increase(candid_asset_amount) => {
                BalanceChanged::Increase(candid_asset_amount.0)
            }
            CandidBalanceChanged::Decrease(candid_asset_amount) => {
                BalanceChanged::Decrease(candid_asset_amount.0)
            }
        }
    }
}

impl From<BalanceChanged> for CandidBalanceChanged {
    fn from(balance_changed: BalanceChanged) -> Self {
        match balance_changed {
            BalanceChanged::Increase(asset_amount) => {
                CandidBalanceChanged::Increase(asset_amount.into())
            }
            BalanceChanged::Decrease(asset_amount) => {
                CandidBalanceChanged::Decrease(asset_amount.into())
            }
        }
    }
}
