use br_reqwest::Client;
use std::collections::{HashMap};
use base64::{Engine};
use base64::engine::general_purpose::STANDARD;
use chrono::{Local};
use crate::{PayMode, PayNotify, RefundNotify, RefundStatus, TradeState, TradeType, Types};
use json::{array, object, JsonValue};
use openssl::hash::MessageDigest;
use openssl::pkey::{PKey};
use openssl::rsa::Rsa;
use openssl::sign::{Signer};
use urlencoding::{encode};

#[derive(Clone, Debug)]
pub struct AliPay {
    /// 应用appid
    pub appid: String,
    /// 服务商商家号
    pub sp_mchid: String,
    /// 授权token
    pub app_auth_token: String,
    /// 应用私钥证书路径
    pub app_private: String,
    /// 接口内容加密密钥
    pub content_encryp: String,
    /// 支付宝公钥
    pub alipay_public_key: String,
    pub notify_url: String,
    /// 第三方关联小程序
    pub appid_mini: String,
}
impl AliPay {
    pub fn sign(&mut self, txt: &str) -> Result<JsonValue, String> {
        let t = self.app_private.as_bytes().chunks(64).map(|chunk| std::str::from_utf8(chunk).unwrap_or("")).collect::<Vec<&str>>().join("\n");
        if t.is_empty() {
            return Err("No pem".to_string());
        }
        let cart = format!("-----BEGIN PRIVATE KEY-----\n{t}\n-----END PRIVATE KEY-----\n");

        // 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 编码输出
        Ok(STANDARD.encode(signature).into())
    }
    pub fn http(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
        let mut http = Client::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(),
                    "app_private_key":self.app_private.clone(),
                    "version":"1.0",
                    "sign_type":"RSA2",
                    "timestamp":timestamp,
                    "alipay_public_key":self.alipay_public_key.clone(),
                    "sign":sign
        };
        if !self.app_auth_token.is_empty() {
            data["app_auth_token"] = self.app_auth_token.clone().into();
        }
        if method.contains("alipay.trade.") {
            data["notify_url"] = self.notify_url.clone().into();
        }
        for (key, value) in biz_content.entries() {
            data[key] = value.clone()
        }
        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("&");
        data["sign"] = self.sign(&txt)?;

        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 method {
            "alipay.trade.wap.pay" => {
                let tt = http.get(url.as_str()).query(data);
                return Ok(tt.url.clone().into());
            }
            _ => {
                match http.get(&url).query(data).form_data(biz_content).send() {
                    Ok(e) => e,
                    Err(e) => return Err(e.to_string())
                }
            }
        };

        let res = res.json()?;
        if res.has_key("error_response") {
            return Err(res["error_response"]["sub_msg"].to_string());
        }
        let key = method.replace(".", "_");
        let key = format!("{key}_response");
        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())
        }
    }
    pub fn https(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
        let mut http = Client::new();
        //http.debug();
        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(),
                    "app_private_key":self.app_private.clone(),
                    "version":"1.0",
                    "sign_type":"RSA2",
                    "timestamp":timestamp,
                    "alipay_public_key":self.alipay_public_key.clone(),
                    "sign":sign
        };
        if !self.app_auth_token.is_empty() {
            data["app_auth_token"] = self.app_auth_token.clone().into();
        }
        if method.contains("alipay.trade.") {
            data["notify_url"] = self.notify_url.clone().into();
        }
        for (key, value) in biz_content.entries() {
            data[key] = value.clone()
        }
        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("&");
        data["sign"] = self.sign(&txt)?;

        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 method {
            "alipay.trade.wap.pay" => {
                let tt = http.get(url.as_str()).query(data);
                return Ok(tt.url.clone().into());
            }
            _ => {
                match http.get(&url).query(data).form_data(biz_content).send() {
                    Ok(e) => e,
                    Err(e) => return Err(e.to_string())
                }
            }
        };

        let res = res.json()?;
        if res.has_key("error_response") {
            return Err(res["error_response"]["sub_msg"].to_string());
        }
        let key = method.replace(".", "_");
        let key = format!("{key}_response");
        let data = res[key].clone();
        Ok(data)
    }
}
impl PayMode for AliPay {
    fn check(&mut self) -> Result<bool, String> {
        let biz_content = object! {
            "biz_content":{
                "grant_type":"authorization_code",
                "code":"123456"
            }
        };
        match self.http("alipay.open.auth.token.app", biz_content) {
            Ok(e) => e,
            Err(e) => {
                if e.contains("auth_code不存在") {
                    return Ok(true);
                }
                return Err(e);
            }
        };
        Ok(true)
    }

    fn get_sub_mchid(&mut self, sub_mchid: &str) -> Result<JsonValue, String> {
        let res = self.https("alipay.open.agent.signstatus.query", object! {
            "biz_content":{
                "pid":sub_mchid,
                "product_codes":array!["QUICK_WAP_WAY"]
            }
        })?;
        if !res["code"].eq("10000") {
            return Err(res["msg"].to_string());
        }
        for item in res["sign_status_list"].members() {
            if item["status"].eq("none") {
                return Err(format!("{} 未开通", res["product_name"]));
            }
        }
        Ok(true.into())
    }


    fn config(&mut self) -> JsonValue {
        todo!()
    }


    fn pay(&mut self, _channel: &str, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
        let mut api = "";
        let mut order = object! {
            out_trade_no:out_trade_no,
            total_amount:total_fee,
            subject:description,
            product_code:"JSAPI_PAY",
            op_app_id:sub_mchid,
            buyer_open_id:""
        };
        match types {
            Types::MiniJsapi => {
                api = "alipay.trade.create";
                order["product_code"] = "JSAPI_PAY".into();
                order["op_app_id"] = self.appid_mini.clone().into();
                if sp_openid.starts_with("2088") {
                    order["buyer_id"] = sp_openid.into();
                } else {
                    order["op_buyer_open_id"] = sp_openid.into();
                }
            }
            Types::Jsapi => {
                api = "alipay.trade.wap.pay";
                order["product_code"] = "QUICK_WAP_WAY".into();
            }
            Types::H5 => {
                api = "alipay.trade.wap.pay";
                order["product_code"] = "QUICK_WAP_WAY".into();
            }
            Types::Native => {
                api = "alipay.trade.wap.pay";
                order["product_code"] = "QUICK_WAP_WAY".into();
            }
            _ => {
                order["product_code"] = "JSAPI_PAY".into();
            }
        };
        match self.http(api, object! {"biz_content":order}) {
            Ok(e) => {
                match types {
                    Types::Native => {}
                    Types::Jsapi | Types::H5 => {
                        return Ok(object! {url:e});
                    }
                    Types::MiniJsapi => {
                        return Ok(e);
                    }
                    Types::App => {}
                    Types::Micropay => {}
                }
                Ok(e)
            }
            Err(e) => {
                if e == "ACCESS_FORBIDDEN" {
                    return Err("请商户授权JSAPI 绑定 服务商小程序APPID".to_string());
                }
                println!("Err: {e:#}");
                Err(e)
            }
        }
    }

    fn micropay(&mut self, _channel: &str, auth_code: &str, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, org_openid: &str, _ip: &str) -> Result<JsonValue, String> {
        let order = object! {
            out_trade_no:out_trade_no,
            total_amount:total_fee,
            subject:description,
            seller_id:sub_mchid,
            auth_code:auth_code,
            scene:"bar_code",
            operator_id:org_openid,
        };
        match self.http("alipay.trade.pay", object! {"biz_content":order}) {
            Ok(e) => {
                println!("alipay.trade.pay:{e:#}");
                if e["code"].ne("10000") {
                    return Err(e["msg"].to_string());
                }
                if e["msg"].eq("FAIL") {
                    if e["err_code_des"].ne("需要用户输入支付密码") {
                        return Err(e["err_code_des"].to_string());
                    }
                    let res = PayNotify {
                        trade_type: TradeType::MICROPAY,
                        out_trade_no: out_trade_no.to_string(),
                        sp_mchid: self.sp_mchid.clone(),
                        sub_mchid: sub_mchid.to_string(),
                        sp_appid: self.appid.to_string(),
                        transaction_id: "".to_string(),
                        success_time: 0,
                        sp_openid: "".to_string(),
                        sub_openid: "".to_string(),
                        total: total_fee,
                        payer_total: total_fee,
                        currency: "CNY".to_string(),
                        payer_currency: "CNY".to_string(),
                        trade_state: TradeState::NOTPAY,
                    };
                    return Ok(res.json());
                }
                let res = PayNotify {
                    trade_type: TradeType::MICROPAY,
                    out_trade_no: out_trade_no.to_string(),
                    sp_mchid: self.sp_mchid.clone(),
                    sub_mchid: sub_mchid.to_string(),
                    sp_appid: self.appid.to_string(),
                    transaction_id: e["trade_no"].to_string(),
                    success_time: PayNotify::datetime_to_timestamp(e["gmt_payment"].as_str().unwrap(), "%Y-%m-%d %H:%M:%S"),
                    sp_openid: e["buyer_open_id"].to_string(),
                    sub_openid: e["buyer_open_id"].to_string(),
                    total: total_fee,
                    payer_total: total_fee,
                    currency: "CNY".to_string(),
                    payer_currency: "CNY".to_string(),
                    trade_state: TradeState::SUCCESS,
                };
                Ok(res.json())
            }
            Err(e) => Err(e)
        }
    }


    fn close(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
        let order = object! {
              "biz_content"=> object! {
                out_trade_no:out_trade_no,
                operator_id:sub_mchid
              }
        };
        match self.http("alipay.trade.close", order) {
            Ok(_) => {
                Ok(true.into())
            }
            Err(e) => {
                if e.contains("交易不存在") {
                    return Ok(true.into());
                }
                Err(e)
            }
        }
    }

    fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
        let order = object! {
              "biz_content"=> object! {
               out_trade_no:out_trade_no
              }
        };
        match self.http("alipay.trade.query", order) {
            Ok(e) => {
                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
                    return Err(e["msg"].to_string());
                }
                let buyer_open_id = if e.has_key("buyer_open_id") {
                    e["buyer_open_id"].to_string()
                } else {
                    e["buyer_user_id"].to_string()
                };
                let send_pay_date = e["send_pay_date"].as_str().unwrap_or("");
                let success_time = if !send_pay_date.is_empty() {
                    PayNotify::alipay_time(send_pay_date)
                } else {
                    0
                };
                let res = PayNotify {
                    trade_type: TradeType::None,
                    out_trade_no: e["out_trade_no"].to_string(),
                    sp_mchid: "".to_string(),
                    sub_mchid: sub_mchid.to_string(),
                    sp_appid: "".to_string(),
                    transaction_id: e["trade_no"].to_string(),
                    success_time,
                    sp_openid: buyer_open_id.clone(),
                    sub_openid: buyer_open_id.clone(),
                    total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
                    payer_total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
                    currency: "CNY".to_string(),
                    payer_currency: "CNY".to_string(),
                    trade_state: TradeState::from(e["trade_status"].as_str().unwrap()),
                };
                Ok(res.json())
            }
            Err(e) => Err(e)
        }
    }

    fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str, _channel: &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> {
        let body = object! {
            "biz_content"=> object! {
                "trade_no"=>transaction_id,
                "out_trade_no"=>out_trade_no,
                "out_request_no"=>out_refund_no,
                "refund_amount"=>format!("{:.2}",amount),
            }
        };
        match self.http("alipay.trade.refund", body.clone()) {
            Ok(e) => {
                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
                    return Err(e["msg"].to_string());
                }
                let res = RefundNotify {
                    out_trade_no: e["out_trade_no"].to_string(),
                    refund_no: out_refund_no.to_string(),
                    sp_mchid: "".to_string(),
                    sub_mchid: sub_mchid.to_string(),
                    transaction_id: e["trade_no"].to_string(),
                    refund_id: out_refund_no.to_string(),
                    success_time: PayNotify::alipay_time(e["gmt_refund_pay"].as_str().unwrap()),
                    total,
                    refund: e["refund_fee"].to_string().parse::<f64>().unwrap(),
                    payer_total: e["refund_fee"].to_string().parse::<f64>().unwrap(),
                    payer_refund: e["send_back_fee"].to_string().parse::<f64>().unwrap(),
                    status: RefundStatus::from(e["fund_change"].as_str().unwrap()),
                };
                Ok(res.json())
            }
            Err(e) => Err(e)
        }
    }

    fn micropay_refund(&mut self, _sub_mchid: &str, _out_trade_no: &str, _transaction_id: &str, _out_refund_no: &str, _amount: f64, _total: f64, _currency: &str, _refund_text: &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, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
        let body = object! {
             "biz_content"=> object! {
               "out_request_no"=>out_refund_no,
            "trade_no"=>trade_no,
             }
        };
        match self.http("alipay.trade.fastpay.refund.query", body.clone()) {
            Ok(e) => {
                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
                    return Err(e["msg"].to_string());
                }
                let res = RefundNotify {
                    out_trade_no: e["out_trade_no"].to_string(),
                    refund_no: e["out_request_no"].to_string(),
                    sp_mchid: "".to_string(),
                    sub_mchid: sub_mchid.to_string(),
                    transaction_id: e["trade_no"].to_string(),
                    refund_id: e["out_request_no"].to_string(),
                    success_time: Local::now().timestamp(),
                    total: e["total_amount"].to_string().parse::<f64>().unwrap(),
                    payer_total: e["total_amount"].to_string().parse::<f64>().unwrap(),
                    refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
                    payer_refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
                    status: RefundStatus::from(e["refund_status"].as_str().unwrap()),
                };
                Ok(res.json())
            }
            Err(e) => Err(e)
        }
    }

    fn incoming(&mut self, _business_code: &str, _contact_info: JsonValue, _subject_info: JsonValue, _business_info: JsonValue, _settlement_info: JsonValue, _bank_account_info: JsonValue) -> Result<JsonValue, String> {
        todo!()
    }
}
