1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//use async_trait::async_trait;
use async_session::{Session, SessionStore,MemoryStore};
use async_sqlx_session::SqliteSessionStore;
// use std::sync::{Arc,Mutex};

// use axum::{
//     http::{
//         StatusCode,
//         request::Parts,
//     },
//     extract::FromRequestParts,
// };
// use tower_cookies::Cookies;
// use rand_chacha::ChaCha8Rng;
// pub type Random = Arc<Mutex<ChaCha8Rng>>;

use crate::{
//    SESSION_COOKIE_NAME,
    defs::{
        FileStore,
        User,
        AppDBs,
        SESSION_ID,
        USER_DATA,
    },
};

use super::UserStatus;

#[derive(Clone)]
pub enum SessionStoreDB {
    Files(FileStore),
    SqlLite(SqliteSessionStore),
    Memory(MemoryStore),
    None,
}
/// In `SessionStoreDB` creation match the corresponding store to return it as item argument 
impl SessionStoreDB {
    pub fn connect_file_store(store: FileStore) -> Self{
        SessionStoreDB::Files(store)
    }
    pub fn connect_memory_store() -> Self{
        SessionStoreDB::Memory(MemoryStore::new())
    } 
    pub fn connect_sqlite_store(store: SqliteSessionStore) -> Self{
        SessionStoreDB::SqlLite(store)
    } 
    pub async fn store_session_data(id: &str, user_data: &str, expire: u64, app_dbs: &AppDBs) -> String {
        // dbg!("user_data: {}",&user_data);
        let mut session = Session::new();
        if ! id.is_empty() {
            session.insert(SESSION_ID, id).unwrap_or_else(|e|{
                println!("Error insert session {}",e);
            });
        }
        if ! user_data.is_empty() {
            session.insert(USER_DATA, user_data).unwrap_or_else(|e|{
                println!("Error insert session {}",e);
            });
        }
        if expire > 0 {
            session.expire_in(std::time::Duration::from_secs(expire));
        } else if app_dbs.config.session_expire > 0 {
            session.expire_in(std::time::Duration::from_secs(app_dbs.config.session_expire));
        }
        //session.into_cookie_value().unwrap_or_default()
        match app_dbs.sessions_store_db.to_owned() {
            SessionStoreDB::Files(store) => {
                store.store_session(session).await.unwrap_or_else(|e|{
                    println!("Error store session {}",e);
                    None
                }).unwrap_or_default()
            },
            SessionStoreDB::SqlLite(store) => {
               store.store_session(session).await.unwrap_or_else(|e|{
                    println!("Error store session {}",e);
                    None
                }).unwrap_or_default() 
            },
            SessionStoreDB::Memory(store) => {
                store.store_session(session).await.unwrap_or_else(|e|{
                    println!("Error store session {}",e);
                    None
                }).unwrap_or_default()
            },
            SessionStoreDB::None => {
                String::from("")
            },
        }
    }
    pub async fn update_session_data(session: Session, app_dbs: &AppDBs) -> String {
        //session.into_cookie_value().unwrap_or_default()
        let cur_session = session.clone();
        match app_dbs.sessions_store_db.to_owned() {
            SessionStoreDB::Files(store) => {
                store.store_session(session).await.unwrap_or_else(|e|{
                    println!("Error store session {}",e);
                    None
                }).unwrap_or_default()
            },
            SessionStoreDB::SqlLite(store) => {
                let _ = store.destroy_session(session).await;
                store.store_session(cur_session).await.unwrap_or_else(|e|{
                    println!("Error store session {}",e);
                    None
                }).unwrap_or_default() 
            },
            SessionStoreDB::Memory(store) => {
                store.store_session(session).await.unwrap_or_else(|e|{
                    println!("Error store session {}",e);
                    None
                }).unwrap_or_default()
            },
            SessionStoreDB::None => {
                String::from("")
            },
        }
    }
    pub async fn cleanup_data(app_dbs: &AppDBs) {
        //session.into_cookie_value().unwrap_or_default()
        match app_dbs.sessions_store_db.to_owned() {
            SessionStoreDB::Files(store) => {
                let _ = store.cleanup().await;
            },
            SessionStoreDB::SqlLite(store) => {
                let _ = store.cleanup().await;
            },
            SessionStoreDB::Memory(store) => {
                let _ = store.cleanup().await;
            },
            SessionStoreDB::None => {
            },
        }
    }
}

#[derive(Clone, Default, Debug)]
pub struct AuthState{
    pub session: Option<Session>,
    pub user: Option<User>, 
}
impl AuthState {
    // pub fn new(session: Session, user: User) -> Self {
    //     let sid = session.get("sid").unwrap_or_default();
    //     let user_store = &app_dbs.user_store;
    //     let user = User::select("id", sid, user_store).await.unwrap_or_default();
    //     //let user = User::from_id(sid, app_dbs);
    //     Self {
    //         session,
    //         user
    //     }
    // }
    // pub fn sid(&self) -> String {
    //     if let Some(session) = &self.session {
    //         session.get("sid").unwrap_or_default()
    //     } else {
    //         String::from("")
    //     }
    // }
    pub fn id(&self) -> String {
        if let Some(session) = &self.session {
            session.id().to_owned()
        } else {
            String::from("")
        }
    }
    #[allow(dead_code)]
    pub fn ses_expired(&self) -> bool {
        if let Some(session) = &self.session {
            session.is_expired()
        } else {
            true
        }
    }
    pub fn ses_destroyed(&self) -> bool {
        if let Some(session) = &self.session {
            session.is_destroyed()
        } else {
            true
        }
    }
    pub fn destroy(&self) -> bool {
        if let Some(mut session) = self.session.to_owned() {
            session.destroy();
            self.ses_destroyed()
        } else {
            true
        }
    }
    #[allow(dead_code)]
    pub fn ses_validate(&self) -> bool {
        if let Some(session) = &self.session {
            session.clone().validate().is_some()
        } else {
            false
        }
    }
    pub fn user_data(&self) -> Vec<String> {
        if let Some(session) = &self.session {
            let user_data: String = session.get(USER_DATA).unwrap_or_default();
            user_data.split("|").map(|s| s.to_string()).collect()
        } else {
            Vec::new()
        }
    }
    pub fn user_id(&self) -> String {
        let user_data = self.user_data();
        if user_data.len() > 0 && user_data[0] != "0" {
            user_data[0].to_owned()
        } else {
            String::from("")
        }
    }
    pub fn user_name(&self) -> String {
        let user_data = self.user_data();
        if user_data.len() > 1 && user_data[1] != "" {
            user_data[1].to_owned()
        } else {
            String::from("")
        }
    }
    pub fn user_email(&self) -> String {
        let user_data = self.user_data();
        if user_data.len() > 2 && user_data[2] != "" {
            user_data[2].to_owned()
        } else {
            String::from("")
        }
    }
    pub fn user_roles(&self) -> String {
        let user_data = self.user_data();
        if user_data.len() > 3 && user_data[3] != "" {
            user_data[3].to_owned()
        } else {
            String::from("")
        }
    } 
    pub fn user_items(&self) -> String {
        let user_data = self.user_data();
        if user_data.len() > 4 && user_data[4] != "" {
            user_data[4].to_owned()
        } else {
            String::from("")
        }
    } 
    pub fn is_admin(&self) -> bool {
        let user_data = self.user_data();
        if user_data.len() > 5 && user_data[5] != "" {
            user_data[5] == "true"  || user_data[5] == "1"
        } else {
            false
        }
    } 
    #[allow(dead_code)]
    pub fn user_status(&self) -> UserStatus {
        let user_data = self.user_data();
        if user_data.len() > 6 && user_data[6] != "" {
            UserStatus::from_str(&user_data[6])
        } else {
            UserStatus::Unknown
        }
    } 
    pub async fn expire_in(&mut self, expire_secs: u64, app_dbs: &AppDBs ) -> Self {
        if let Some(mut session) = self.session.to_owned() {
            session.expire_in(std::time::Duration::from_secs(expire_secs));
            let _ = SessionStoreDB::update_session_data(session.to_owned(),&app_dbs).await;
            Self {
                session: Some(session),
                user: self.user.to_owned(),
            }
        } else {
            self.to_owned()
        }
    } 
    #[allow(dead_code)]
    pub async fn from_data(&self, app_dbs: &AppDBs) -> Self {
        let user_id = self.user_id();
        let user = if !user_id.is_empty() {
            // dbg!(&user_id);
            match User::select("id", &user_id, false, &app_dbs.user_store).await {
                Ok(user) => Some(user),
                Err(e) => {
                    println!("Error user {}: {:#}",&user_id,e);
                    None
                }
            }
        } else {
            None
        };
        // dbg!(&user);
        Self {
            session: self.session.to_owned(),
            user,
        }
    }
    pub async fn from_cookie(session_cookie: String, app_dbs: &AppDBs) -> Self {
        let result_session = match app_dbs.sessions_store_db.to_owned() {
            SessionStoreDB::Files(store) => 
                store
                    .load_session(session_cookie)
                    .await
            ,
            SessionStoreDB::SqlLite(store) =>
                store
                    .load_session(session_cookie)
                    .await
            ,
            SessionStoreDB::Memory(store) => 
                store
                    .load_session(session_cookie.to_string())
                    .await
            ,
            SessionStoreDB::None => {
                dbg!("No store");
                return AuthState::default();
            },
        };
        // dbg!(&result_session);
        match result_session {
            Ok(op_session) => if let Some(session) = op_session {
                if let Some(sid) = session.get::<String>("sid") {
                    // dbg!(
                    //     "UserDataFromSession: session decoded success, user_data={}",
                    //     &session
                    // );
                    let user_store = &app_dbs.user_store;
                    let user = User::select("id", &sid, false, user_store).await.unwrap_or_default();
                    return AuthState {
                        session: Some(session),
                        user: Some(user)
                    };
                } else {
                    println!("No `sid` found in session");
                }
            },
            Err(e) => {
                println!("Error session {}", e);
            }
        }  
        AuthState::default()
    }
}

/*
#[async_trait]
impl<S> FromRequestParts<S> for AuthState
where
    S: Send + Sync,
{
    type Rejection = (StatusCode, &'static str);

    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
        // let h = parts.headers.to_owned();
        // dbg!(&h);
/*
        if let Some(app_dbs) = parts.extensions.get::<Arc<AppDBs>>()  {
            if let Some(cookies) = parts.extensions.get::<Cookies>()  {
                if let Some(s_cookie) = cookies.get(SESSION_COOKIE_NAME) {
                    let session_cookie = s_cookie.to_string().replace(&format!("{}=",SESSION_COOKIE_NAME),"");
                    println!("From request parts cookie: {}",session_cookie);
                    // continue to decode the session cookie
                    // return Ok(
                    //     AuthState::from_cookie(session_cookie, app_dbs).await
                    // );           
                } 
            }
        }
        */
        Ok(AuthState::default())
        // Err((
        //     StatusCode::INTERNAL_SERVER_ERROR,
        //     "No `user_data` found in session",
        // ))
        // let cookie = Option::<Cookie>::g(parts, state);
        // let cookie = Cookies::get(parts, state);
        // if let Some(_user_agent) = parts.headers.get(USER_AGENT) {
        //     Ok(AuthState(None))
        // } else {
        //     Err((StatusCode::BAD_REQUEST, "`User-Agent` header is missing"))
        // }
//         let cookie = Option::<TypedHeader<Cookie>>::from_request_parts(req, state)
        //let cookies = Cookies::from(pa, state).await?;

//         let info: String = cookies
//             .get(SESSION_COOKIE_NAME)
// //            .and_then(|c| c.value().parse().ok())
//             .unwrap_or_default()
//             ;
//         dbg!(&info);
//         //cookies.add(Cookie::new(COOKIE_NAME, visited.to_string()));
     }
}
*/