use json::{JsonValue, object};
use crate::{Field};
use crate::datetime::Datetime;
pub struct Str {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub dec: String,
    pub show: bool,
    pub describe: String,
}
impl Str {
    pub fn new(require: bool, field: &str, title: &str, length: i32, default: &str) -> Str {
        Self {
            require,
            field: field.to_string(),
            mode: "string".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length,
            dec: "".to_string(),
            show: true,
            describe: "".to_string(),
        }
    }
}
impl Field for Str {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("`{}` varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field
    }
    fn show(&mut self, open: bool) -> &mut Self {
        self.show = open;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Pass {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub dec: String,
    pub show: bool,
    pub describe: String,
}
impl Pass {
    pub fn new(require: bool, field: &str, title: &str, length: i32, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "pass".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length,
            dec: "".to_string(),
            show: true,
            describe: "".to_string(),
        }
    }
}
impl Field for Pass {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("`{}` varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field
    }
    fn show(&mut self, open: bool) -> &mut Self {
        self.show = open;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Key {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub dec: String,
    pub auto: bool,
    pub show: bool,
    pub describe: String,
}
impl Key {
    pub fn new(require: bool, field: &str, title: &str, length: i32) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "key".to_string(),
            title: title.to_string(),
            def: "".to_string(),
            length,
            dec: "".to_string(),
            auto: false,
            show: true,
            describe: "".to_string(),
        }
    }
    pub fn auto(mut self) -> Self {
        self.length = 0;
        self.auto = true;
        self
    }
}
impl Field for Key {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = "".to_string();
        match model {
            "sqlite" => {
                match self.auto {
                    true => {
                        sql = format!("{}{} INTEGER PRIMARY KEY", sql.clone(), self.field);
                        sql = format!("{} AUTOINCREMENT", sql.clone());
                        sql
                    }
                    false => {
                        sql = format!("{}{}  varchar({})", sql.clone(), self.field, self.length);
                        sql = format!("{}", sql.clone());
                        sql
                    }
                }
            }
            _ => {
                match self.auto {
                    true => {
                        sql = format!("{}{} int({})", sql.clone(), self.field, self.length);
                        sql = format!("{} not null AUTO_INCREMENT", sql.clone());
                        format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length)
                    }
                    false => {
                        sql = format!("{}{}  varchar({})", sql.clone(), self.field, self.length);
                        sql = format!("{}", sql.clone());
                        format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def, self.dec)
                    }
                }
            }
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field.insert("auto", JsonValue::from(self.auto.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field
    }
    fn show(&mut self, open: bool) -> &mut Self {
        self.show = open;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Tel {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub show: bool,
    pub describe: String,
}
impl Tel {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "tel".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 15,
            show: true,
            describe: "".to_string(),
        }
    }
}
impl Field for Tel {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field
    }
    fn show(&mut self, open: bool) -> &mut Self {
        self.show = open;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Email {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub show: bool,
    pub describe: String,
}
impl Email {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "email".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 50,
            show: true,
            describe: "".to_string(),
        }
    }
}
impl Field for Email {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field
    }
    fn verify(&mut self, data: JsonValue) -> JsonValue {
        data
    }
    fn show(&mut self, open: bool) -> &mut Self {
        self.show = open;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Color {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: i32,
    pub show: bool,
    pub describe: String,
}
impl Color {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "color".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 9,
            show: true,
            describe: "".to_string(),
        }
    }
}
impl Field for Color {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field
    }
    fn verify(&mut self, data: JsonValue) -> JsonValue {
        data
    }
    fn show(&mut self, open: bool) -> &mut Self {
        self.show = open;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Code {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: String,
    pub length: usize,
    pub show: bool,
    pub describe: String,
    pub dec: String,
}
impl Code {
    pub fn new(require: bool, field: &str, title: &str, length: usize, dec: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "code".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: length + dec.len(),
            show: true,
            describe: "".to_string(),
            dec: dec.to_string(),
        }
    }
    pub fn order_no(prefix: &str) -> String {
        let data = Datetime::datetime_format("%Y%m%d%H%M%S%f");
        let data = data.trim_end_matches("000");
        format!("{}{}", prefix, data)
    }
    pub fn code_no(prefix: &str) -> String {
        let data = Datetime::datetime_format("%Y%m%d%H%M%f");
        let data = data.trim_end_matches("000");
        format!("{}{}", prefix, data)
    }
    pub fn batch_no(prefix: &str) -> String {
        let data = Datetime::datetime_format("%Y%m%d%H");
        format!("{}{}", prefix, data)
    }
}
impl Field for Code {
    fn sql(&mut self, model: &str) -> String {
        let mut sql = format!("{} varchar({})", self.field, self.length);
        if self.require {
            sql = format!("{} not null", sql.clone())
        };
        sql = format!("{} default '{}'", sql.clone(), self.def);
        match model {
            "sqlite" => sql,
            _ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.dec, self.def)
        }
    }
    fn field(&mut self) -> JsonValue {
        let mut field = object! {};
        field.insert("require", JsonValue::from(self.require.clone())).unwrap();
        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
        field.insert("length", JsonValue::from(self.length.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
        field
    }
    fn show(&mut self, open: bool) -> &mut Self {
        self.show = open;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}