use scraper::{Html, Selector};

use super::session::{classroom_post_redirect, request};

pub async fn update_expiration_date(
    user_id: i32,
    new_expiration_date: String,
) -> Result<(), String> {
    let html = request(format!("/main/admin/user_edit.php?user_id={}", user_id)).await?;

    let req_body = {
        let html = Html::parse_document(&html);

        let surnames_selector =
            Selector::parse("#user_edit_lastname").expect("Error creating surnames selector");
        let names_selector =
            Selector::parse("#user_edit_firstname").expect("Error creating surnames selector");
        let email_selector =
            Selector::parse("#user_edit_email").expect("Error creating surnames selector");
        let username_selector =
            Selector::parse("#user_edit_username").expect("Error creating surnames selector");
        let protect_token_sel = Selector::parse("#user_edit_protect_token")
            .expect("Error creating protect_token selector");

        let surnames = html
            .select(&surnames_selector)
            .next()
            .ok_or(format!("surnames element not found"))?
            .value()
            .attr("value")
            .ok_or(format!("surnames input has no value attribute"))?;

        let names = html
            .select(&names_selector)
            .next()
            .ok_or(format!("names element not found"))?
            .value()
            .attr("value")
            .ok_or(format!("names input has no value attribute"))?;

        let email = html
            .select(&email_selector)
            .next()
            .ok_or(format!("email element not found"))?
            .value()
            .attr("value")
            .ok_or(format!("email input has no value attribute"))?;

        let username = html
            .select(&username_selector)
            .next()
            .ok_or(format!("username element not found"))?
            .value()
            .attr("value")
            .ok_or(format!("username input has no value attribute"))?;

        let protect_token = html
            .select(&protect_token_sel)
            .next()
            .ok_or(format!("protect_token element not found"))?
            .value()
            .attr("value")
            .ok_or(format!("protect_token input has no value attribute"))?;

        get_body(
            surnames.into(),
            names.into(),
            email.into(),
            username.into(),
            new_expiration_date,
            user_id,
            protect_token.into(),
        )
    };

    classroom_post_redirect(
        "/main/admin/user_edit.php".into(),
        "multipart/form-data; boundary=---------------------------318235432819784070062970146417"
            .into(),
        req_body,
    )
    .await
}

fn get_body(
    surnames: String,
    names: String,
    email: String,
    username: String,
    new_expiration_date: String,
    user_id: i32,
    protect_token: String,
) -> String {
    format!(
        r#"
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="lastname"

{surnames}
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="firstname"

{names}
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="official_code"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="email"

{email}
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="phone"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="picture"; filename=""
Content-Type: application/octet-stream


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="username"

{username}
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="reset_password"

0
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="password"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="status"

5
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="platform_admin"

0
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="language"

spanish
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="send_mail"

0
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="radio_expiration_date"

1
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="expiration_date"

{new_expiration_date} 23:00
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="active"

1
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="q"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="q"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_legal_accept"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_already_logged_in"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_update_type"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_rssfeeds"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_dashboard"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_timezone"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_mail_notify_invitation"

1
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_mail_notify_message"

1
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_mail_notify_group_message"

1
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_user_chat_status"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_google_calendar_url"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_captcha_blocked_until_date"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_skype"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_linkedin_url"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_request_for_legal_agreement_consent_removal_justification"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_request_for_delete_account_justification"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_request_for_legal_agreement_consent_removal"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="extra_request_for_delete_account"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="submit"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="_qf__user_edit"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="protect_token"

{protect_token}
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="user_id"

{user_id}
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="MAX_FILE_SIZE"

536870912
-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="picture_crop_result"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="picture_crop_image_base_64"


-----------------------------318235432819784070062970146417
Content-Disposition: form-data; name="item_id"

{user_id}
-----------------------------318235432819784070062970146417--
        "#
    )
}
