Documentation
use std::collections::{HashMap};
use base64::Engine;
use base64::engine::general_purpose;
use chrono::Local;
use crate::PayMode;
use json::{object, JsonValue};
use openssl::hash::MessageDigest;
use openssl::pkey::{PKey};
use openssl::rsa::Rsa;
use openssl::sign::Signer;
use urlencoding::encode;

#[derive(Clone)]
pub struct AliPay {
    /// 应用appid
    pub appid: String,
    /// 服务商商家号
    pub sp_mchid: String,
    /// 授权token
    pub app_auth_token: String,
    /// 应用私钥证书路径
    pub app_private: String,
}
impl AliPay {
    pub fn http(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
        let mut http = br_http::Http::new();
        let sign = "";

        let now = Local::now();
        let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();


        let mut data = object! {
            "charset":"UTF-8",
            "method":method,
            "app_id":self.appid.clone(),
            "version":"1.0",
            "sign_type":"RSA2",
            "timestamp":timestamp,
            "app_auth_token":self.app_auth_token.clone(),
            "sign":sign,
            "biz_content": biz_content,
        };

        let mut map = HashMap::new();
        for (key, value) in data.entries() {
            if key == "sign" {
                continue;
            }
            if value.is_empty() {
                continue;
            }
            map.insert(key, value);
        }

        let mut keys: Vec<_> = map.keys().cloned().collect();
        keys.sort();
        let mut txt = vec![];
        for key in keys {
            txt.push(format!("{}={}", key, map.get(&key).unwrap()));
        }
        let txt = txt.join("&");
        let t = self.app_private.as_bytes().chunks(64).map(|chunk| std::str::from_utf8(chunk).unwrap_or("")).collect::<Vec<&str>>().join("\n");
        let cart = format!("-----BEGIN PRIVATE KEY-----\n{}\n-----END PRIVATE KEY-----\n", t);

        // 1. 加载私钥
        let rsa = match Rsa::private_key_from_pem(cart.as_bytes()) {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };

        let pkey = match PKey::from_rsa(rsa) {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };

        // 2. 创建签名器,使用 SHA256
        let mut signer = match Signer::new(MessageDigest::sha256(), &pkey) {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };
        match signer.update(txt.as_bytes()) {
            Ok(()) => {}
            Err(e) => return Err(e.to_string())
        };
        // 3. 生成签名
        let signature = match signer.sign_to_vec() {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };
        // 4. Base64 编码输出
        data["sign"] = general_purpose::STANDARD.encode(signature).into();

        let mut new_data = object! {};
        for (key, value) in data.entries() {
            let t = encode(value.to_string().as_str()).to_string();
            new_data[key] = t.into();
        }
        data = new_data;
        let url = "https://openapi.alipay.com/gateway.do".to_string();
        let res = match http.post(url.as_str()).query(data).json() {
            Ok(e) => e,
            Err(e) => {
                println!(">>>>>>{}", e);
                return Err(e);
            }
        };
        if res.has_key("error_response") {
            return Err(res["error_response"]["sub_msg"].to_string());
        }
        let key = method.replace(".", "_");
        let key = format!("{}_response", key);
        let data = res[key].clone();
        if data.has_key("code") {
            if data["code"] != "10000" {
                Err(data["sub_msg"].to_string())
            } else {
                Ok(data)
            }
        } else {
            Err(data.to_string())
        }
    }
}
impl PayMode for AliPay {
    fn config(&mut self) -> JsonValue {
        todo!()
    }

    fn login(&mut self, code: &str) -> Result<JsonValue, String> {
        let res = self.http("alipay.system.oauth.token", object! {
            grant_type:"authorization_code",
            code:code
        })?;
        Ok(res)
    }

    fn auth(&mut self, code: &str) -> Result<JsonValue, String> {
        let res = match self.http("alipay.open.auth.token.app", object! {
            grant_type:"authorization_code",
            code:code
        }) {
            Ok(e) => e,
            Err(e) => {
                println!("Err: {:#}", e);
                return Err(e);
            }
        };
        //let user_id = res["user_id"].clone();
        //let auth_app_id = res["auth_app_id"].clone();
        //let re_expires_in = res["re_expires_in"].clone();
        //let expires_in = res["expires_in"].clone();
        //let app_auth_token = res["app_auth_token"].clone();
        println!("{:#}", res);
        Ok(object! {})
    }

    fn jsapi(
        &mut self,
        _sub_mchid: &str,
        out_trade_no: &str,
        description: &str,
        total_fee: f64,
        _notify_url: &str,
        sp_openid: &str,
    ) -> Result<JsonValue, String> {
        self.app_auth_token = "202503BB5aba0791592f4cd9966ba090d36dfX11".to_string();
        match self.http("alipay.trade.create", object! {
            out_trade_no:out_trade_no,
            total_amount:total_fee,
            subject:description,
            product_code:"JSAPI_PAY",
            op_app_id:"2088541900270114",
            buyer_open_id:sp_openid
        }) {
            Ok(e) => {
                println!("{:#}", e);
            }
            Err(e) => {
                println!("Err: {:#}", e);
                return Err(e);
            }
        };


        Ok(object! {})
    }

    fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn refund(&mut self, _sub_mchid: &str, _out_trade_no: &str, _transaction_id: &str, _out_refund_no: &str, _amount: f64, _total: f64, _currency: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn refund_query(&mut self, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
        todo!()
    }
}