# -*- coding: utf-8 -*-
# Part of SmartGo. See LICENSE file for full copyright and licensing details.
import logging

from odoo import api, fields, models
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)


class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    token = fields.Char(string='TOKEN', default='b811ac06-3f00-11ed-9aed-0242ac110003')
    sf_secret_key = fields.Char(string='密钥', default='wBmxej38OkErKhD6')
    sf_url = fields.Char(string='访问地址', default='https://sf.cs.jikimo.com')
    ftp_host = fields.Char(string='FTP的ip')
    ftp_port = fields.Char(string='FTP端口')
    ftp_user = fields.Char(string='FTP用户')
    ftp_password = fields.Char(string='FTP密码')

    def sf_all_sync(self):
        self.env['sf.production.materials'].sync_all_production_materials()
        _logger.info("同步资源库材料")
        self.env['sf.materials.model'].sync_all_materials_model()
        _logger.info("同步资源库材料型号")
        self.env['sf.production.process'].sync_all_production_process()
        _logger.info("同步资源库表面工艺")
        self.env['sf.processing.technology'].sync_all_processing_technology()
        _logger.info("同步资源库加工工艺")
        self.env['sf.machine.brand.tags'].sync_all_machine_brand_tags()
        _logger.info("同步资源库品牌类别")
        self.env['sf.machine.brand'].sync_all_machine_brand()
        _logger.info("同步资源库品牌")
        self.env['sf.machine.control_system'].sync_all_machine_tool_type_control_system()
        _logger.info("同步资源库控制系统")
        # self.env['sf.machine_tool'].sync_all_machine_tool()
        # _logger.info("同步机床行业编码")
        self.env['sf.machine_tool.type'].sync_all_machine_tool_type()
        _logger.info("同步资源库机床型号")
        self.env['sf.cutting_tool.category'].sync_all_cutting_tool_category()
        _logger.info("同步资源库刀具类别")
        self.env['sf.cutting_tool.type'].sync_all_cutting_tool_type()
        _logger.info("同步资源库刀具")
        # self.env['sf.processing.order'].sync_all_processing_order()

    @api.model
    def get_values(self):
        """
        重载获取参数的方法，参数都存在系统参数中
        :return:
        """
        values = super(ResConfigSettings, self).get_values()
        config = self.env['ir.config_parameter'].sudo()
        token = config.get_param('token', default='')
        sf_secret_key = config.get_param('sf_secret_key', default='')
        sf_url = config.get_param('sf_url', default='')
        ftp_host = config.get_param('ftp_host', default='')
        ftp_port = config.get_param('ftp_port', default='')
        ftp_user = config.get_param('ftp_user', default='')
        ftp_password = config.get_param('ftp_password', default='')

        values.update(
            token=token,
            sf_secret_key=sf_secret_key,
            sf_url=sf_url,
            ftp_host=ftp_host,
            ftp_port=ftp_port,
            ftp_user=ftp_user,
            ftp_password=ftp_password
        )
        return values

    def set_values(self):
        super(ResConfigSettings, self).set_values()
        ir_config = self.env['ir.config_parameter'].sudo()
        ir_config.set_param("token", self.token or "")
        ir_config.set_param("sf_secret_key", self.sf_secret_key or "")
        ir_config.set_param("sf_url", self.sf_url or "")
        ir_config.set_param("ftp_host", self.ftp_host or "")
        ir_config.set_param("ftp_port", self.ftp_port or "")
        ir_config.set_param("ftp_user", self.ftp_user or "")
        ir_config.set_param("ftp_password", self.ftp_password or "")
