use json::{JsonValue, object};
use crate::Field;
pub struct Text {
    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 Text {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "text".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 0,
            dec: "".to_string(),
            show: true,
            describe: "".to_string(),
        }
    }
    pub fn medium(mut self) -> Self {
        self.length = 1;
        self
    }
    pub fn long(mut self) -> Self {
        self.length = 2;
        self
    }
}
impl Field for Text {
    fn sql(&mut self, model: &str) -> String {
        match model {
            "sqlite" => {
                format!("`{}` text", self.field)
            }
            _ => {
                let sql = match self.length {
                    1 => format!("`{}` MEDIUMTEXT", self.field),
                    2 => format!("`{}` LONGTEXT", self.field),
                    _ => format!("`{}` text", self.field)
                };
                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 hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Editor {
    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 Editor {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "editor".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 0,
            dec: "".to_string(),
            show: true,
            describe: "".to_string(),
        }
    }
    pub fn medium(mut self) -> Self {
        self.length = 1;
        self
    }
    pub fn long(mut self) -> Self {
        self.length = 2;
        self
    }
}
impl Field for Editor {
    fn sql(&mut self, model: &str) -> String {
        match model {
            "sqlite" => {
                format!("`{}` text", self.field)
            }
            _ => {
                let sql = match self.length {
                    1 => format!("`{}` MEDIUMTEXT", self.field),
                    2 => format!("`{}` LONGTEXT", self.field),
                    _ => format!("`{}` text", self.field)
                };
                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 hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Table {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub table: String,
    pub fields: Vec<String>,
    pub api: String,
    pub def: String,
    pub show: bool,
    pub describe: String,
    pub multiple: bool,
    pub multiple_count: i32,
}
impl Table {
    pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "table".to_string(),
            title: title.to_string(),
            table: table.to_string(),
            api: api.to_string(),
            show: true,
            fields: fields.iter().map(|c| c.to_string()).collect(),
            describe: "".to_string(),
            def: "".to_string(),
            multiple: false,
            multiple_count: 1,
        }
    }
    pub fn multiple(&mut self, count: i32) -> &mut Table {
        if count > 1 {
            self.multiple = true;
        } else {
            self.multiple = false;
        }
        self.multiple_count = count;
        self
    }
}
impl Field for Table {
    fn sql(&mut self, model: &str) -> String {
        match model {
            "sqlite" => {
                format!("{} text", self.field)
            }
            _ => {
                let sql = format!("{} varchar({}) not null  default '{}'", self.field, {
                    if self.multiple {
                        64 * self.multiple_count
                    } else {
                        64
                    }
                }, self.def);
                format!("{} comment '{}|{}|{}|{}|{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.table, self.fields.join("|"), self.api, self.def, self.multiple, self.multiple_count)
            }
        }
    }
    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("table", JsonValue::from(self.table.clone())).unwrap();
        field.insert("api", JsonValue::from(self.api.clone())).unwrap();
        field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
        field.insert("multiple", JsonValue::from(self.multiple.clone())).unwrap();
        field.insert("multiple_count", JsonValue::from(self.multiple_count.clone())).unwrap();
        field.insert("show", JsonValue::from(self.show.clone())).unwrap();
        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
        field
    }
    fn hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Json {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: JsonValue,
    pub length: i32,
    pub show: bool,
    pub describe: String,
}
impl Json {
    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "json".to_string(),
            title: title.to_string(),
            def,
            length: 0,
            show: true,
            describe: "".to_string(),
        }
    }
    pub fn medium(mut self) -> Self {
        self.length = 1;
        self
    }
    pub fn long(mut self) -> Self {
        self.length = 2;
        self
    }
}
impl Field for Json {
    fn sql(&mut self, model: &str) -> String {
        match model {
            "sqlite" => {
                format!("`{}` text", self.field)
            }
            _ => {
                let sql = match self.length {
                    1 => format!("`{}` MEDIUMTEXT", self.field),
                    2 => format!("`{}` LONGTEXT", self.field),
                    _ => format!("`{}` text", self.field)
                };
                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def.to_string())
            }
        }
    }
    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("table", JsonValue::from(self.def.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 hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Array {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: JsonValue,
    pub length: i32,
    pub show: bool,
    pub describe: String,
}
impl Array {
    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "array".to_string(),
            title: title.to_string(),
            def,
            length: 0,
            show: true,
            describe: "".to_string(),
        }
    }
    pub fn medium(mut self) -> Self {
        self.length = 1;
        self
    }
    pub fn long(mut self) -> Self {
        self.length = 2;
        self
    }
}
impl Field for Array {
    fn sql(&mut self, model: &str) -> String {
        match model {
            "sqlite" => {
                format!("`{}` text", self.field)
            }
            _ => {
                let sql = match self.length {
                    1 => format!("`{}` MEDIUMTEXT", self.field),
                    2 => format!("`{}` LONGTEXT", self.field),
                    _ => format!("`{}` text", self.field)
                };
                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def.to_string())
            }
        }
    }
    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("table", JsonValue::from(self.def.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 hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Object {
    pub require: bool,
    pub field: String,
    pub mode: String,
    pub title: String,
    pub def: JsonValue,
    pub length: i32,
    pub show: bool,
    pub describe: String,
}
impl Object {
    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "object".to_string(),
            title: title.to_string(),
            def,
            length: 0,
            show: true,
            describe: "".to_string(),
        }
    }
    pub fn medium(mut self) -> Self {
        self.length = 1;
        self
    }
    pub fn long(mut self) -> Self {
        self.length = 2;
        self
    }
}
impl Field for Object {
    fn sql(&mut self, model: &str) -> String {
        match model {
            "sqlite" => {
                format!("`{}` text", self.field)
            }
            _ => {
                let sql = match self.length {
                    1 => format!("`{}` MEDIUMTEXT", self.field),
                    2 => format!("`{}` LONGTEXT", self.field),
                    _ => format!("`{}` text", self.field)
                };
                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def.to_string())
            }
        }
    }
    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("table", JsonValue::from(self.def.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 hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}
pub struct Url {
    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 Url {
    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
        Self {
            require,
            field: field.to_string(),
            mode: "url".to_string(),
            title: title.to_string(),
            def: default.to_string(),
            length: 0,
            dec: "".to_string(),
            show: true,
            describe: "".to_string(),
        }
    }
}
impl Field for Url {
    fn sql(&mut self, model: &str) -> String {
        let sql = format!("{} text", self.field);
        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 hide(&mut self) -> &mut Self {
        self.show = false;
        self
    }
    fn describe(&mut self, text: &str) -> &mut Self {
        self.describe = text.to_string();
        self
    }
}