use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_elements::events::MouseEvent;
use freya_hooks::{use_animation, use_applied_theme, use_platform, Animation, SwitchThemeWith};
use winit::window::CursorIcon;
#[derive(Props)]
pub struct SwitchProps<'a> {
    pub theme: Option<SwitchThemeWith>,
    pub enabled: bool,
    pub ontoggled: EventHandler<'a, ()>,
}
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum SwitchStatus {
    #[default]
    Idle,
    Hovering,
}
#[allow(non_snake_case)]
pub fn Switch<'a>(cx: Scope<'a, SwitchProps<'a>>) -> Element<'a> {
    let animation = use_animation(cx, || 0.0);
    let theme = use_applied_theme!(cx, &cx.props.theme, switch);
    let platform = use_platform(cx);
    let status = use_ref(cx, SwitchStatus::default);
    use_on_destroy(cx, {
        to_owned![status, platform];
        move || {
            if *status.read() == SwitchStatus::Hovering {
                platform.set_cursor(CursorIcon::default());
            }
        }
    });
    let onmouseleave = {
        to_owned![platform];
        move |_: MouseEvent| {
            *status.write_silent() = SwitchStatus::Idle;
            platform.set_cursor(CursorIcon::default());
        }
    };
    let onmouseenter = move |_: MouseEvent| {
        *status.write_silent() = SwitchStatus::Hovering;
        platform.set_cursor(CursorIcon::Pointer);
    };
    let onclick = |_: MouseEvent| {
        cx.props.ontoggled.call(());
    };
    let (offset_x, border, circle) = {
        if cx.props.enabled {
            (
                animation.value(),
                theme.enabled_background,
                theme.enabled_thumb_background,
            )
        } else {
            (animation.value(), theme.background, theme.thumb_background)
        }
    };
    let _ = use_memo(cx, &cx.props.enabled, move |enabled| {
        if enabled {
            animation.start(Animation::new_sine_in_out(0.0..=25.0, 200));
        } else if animation.value() > 0.0 {
            animation.start(Animation::new_sine_in_out(25.0..=0.0, 200));
        }
    });
    render!(
        rect {
            margin: "1.5",
            width: "50",
            height: "25",
            padding: "1",
            corner_radius: "50",
            background: "{border}",
            onmousedown: |_| {},
            onmouseenter: onmouseenter,
            onmouseleave: onmouseleave,
            onclick: onclick,
            rect {
                width: "100%",
                height: "100%",
                offset_x: "{offset_x}",
                padding: "2.5",
                corner_radius: "50",
                rect {
                    background: "{circle}",
                    width: "18",
                    height: "18",
                    corner_radius: "50",
                }
            }
        }
    )
}