# -*- coding: utf-8 -*-

from application.app import db, ma
from .base import PrimaryModel 
from marshmallow import Schema, fields, INCLUDE, EXCLUDE

class AppModel(PrimaryModel):
    __tablename__ = 'evm_app'

    app_name = db.Column(db.String(70), index = True, nullable = False)
    app_icon = db.Column(db.String(200), nullable = False)
    app_version = db.Column(db.String(20), nullable = False)
    category = db.Column(db.Integer, nullable = False)
    category_2th = db.Column(db.Integer, nullable = False)
    developer = db.Column(db.Integer, nullable = False)
    download_url = db.Column(db.String(20), nullable = False)
    app_file_size = db.Column(db.Integer, nullable = False)
    app_screen_size = db.Column(db.Integer, nullable = False)
    app_arch = db.Column(db.String(20), nullable = False)
    app_review = db.Column(db.String(100), nullable = False)

    # __table_args__ = (
    #     db.Index('idx_xxx', 'xxx', mysql_using='btree'),
    # )

    def __init__(self, app_name, app_icon, app_version, category, category_2th, developer, download_url, app_file_size, app_screen_size, app_arch, app_review):
        self.app_name = app_name
        self.app_icon = app_icon
        self.app_version = app_version
        self.category = category
        self.category_2th = category_2th
        self.developer = developer
        self.download_url = download_url
        self.app_file_size = app_file_size
        self.app_screen_size = app_screen_size
        self.app_arch = app_arch
        self.app_review = app_review

    def __repr__(self):
        return '<AppModel %r>' % (self.app_name)

    def to_json(self):
        return {
            'app_name': self.app_name,
            'app_icon': self.app_icon,
            'app_version': self.app_version,
            'category': self.category,
            'category_2th': self.category_2th,
            'developer': self.developer,
            'download_url': self.download_url,
            'app_file_size': self.app_file_size,
            'app_screen_size': self.app_screen_size,
            'app_arch': self.app_arch,
            'app_review': self.app_review,
        }


class PostAppSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = AppModel

    app_name = ma.auto_field()
    app_icon = ma.auto_field()
    app_version = ma.auto_field()
    category = ma.auto_field()
    category_2th = ma.auto_field()
    developer = ma.auto_field()
    app_screen_size = ma.auto_field()
    app_arch = ma.auto_field()
    app_review = ma.auto_field()

postAppSchema = PostAppSchema()

class DeleteAppSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = AppModel


deleteAppSchema = DeleteAppSchema()

class GetListAppSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = AppModel

    page = fields.Integer(required=False)
    pageSize = fields.Integer(required=False)
    app_name = ma.auto_field()
    app_version = ma.auto_field()
    category = ma.auto_field()
    category_2th = ma.auto_field()
    app_arch = ma.auto_field()

getListAppSchema = GetListAppSchema()
getListAppsSchema = GetListAppSchema(many=True)

class GetAppSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = AppModel

    app_name = ma.auto_field()
    app_version = ma.auto_field()
    category = ma.auto_field()
    category_2th = ma.auto_field()
    app_arch = ma.auto_field()

getAppSchema = GetAppSchema()

class PutAppSchema(ma.SQLAlchemySchema):
    class Meta:
        # unknown = INCLUDE # 未知字段默认包含
        unknown = EXCLUDE # 未知字段默认排除
        model = AppModel

    app_name = ma.auto_field()
    app_icon = ma.auto_field()
    app_version = ma.auto_field()
    app_screen_size = ma.auto_field()
    app_arch = ma.auto_field()
    app_review = ma.auto_field()
    category = ma.auto_field()
    category_2th = ma.auto_field()

putAppSchema = PutAppSchema()
