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

# Copyright 2010-2016 Mir Calculate. http://www.calculate-linux.org
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import sys
from os import path

from calculate.lib.cl_lang import setLocalTranslate

setLocalTranslate('cl_install3', sys.modules[__name__])


class FileSystemManager(object):
    """Convert dict install option"""

    defaultOpt = ['noatime']
    defaultBindOpts = ['bind']
    supportFS = {
        'ext2': {'defaultopt': defaultOpt,
                 'format': '/sbin/mkfs.ext2',
                 'formatparam': '{labelparam} {device}',
                 'gpt': '8300',
                 'label': '-L {labelname}',
                 'ssd': [],
                 'msdos': '83',
                 'type': ['hdd', 'usb-hdd']},
        'ext3': {'defaultopt': defaultOpt,
                 'format': '/sbin/mkfs.ext3',
                 'formatparam': '{labelparam} {device}',
                 'gpt': '8300',
                 'label': '-L {labelname}',
                 'ssd': [],
                 'msdos': '83',
                 'type': ['hdd', 'usb-hdd']},
        'ext4': {'defaultopt': defaultOpt,
                 'format': '/sbin/mkfs.ext4',
                 'formatparam': '{labelparam} {device}',
                 'gpt': '8300',
                 'label': '-L {labelname}',
                 'ssd': ['discard'],
                 'msdos': '83',
                 'type': ['hdd', 'usb-hdd']},
        'reiserfs': {'defaultopt': defaultOpt,
                     'format': '/sbin/mkfs.reiserfs',
                     'formatparam': '{labelparam} -f {device}',
                     'gpt': '8300',
                     'label': '-l {labelname}',
                     'msdos': '83',
                     'ssd': [],
                     'type': ['hdd', 'usb-hdd']},
        'btrfs': {'defaultopt': defaultOpt,
                  'format': '/sbin/mkfs.btrfs',
                  'formatparam': '{labelparam} -f {device}',
                  'gpt': '8300',
                  'label': '-L {labelname}',
                  'msdos': '83',
                  'ssd': ['ssd', 'discard', 'space_cache'],
                  'type': ['hdd', 'usb-hdd']},
        'jfs': {'defaultopt': defaultOpt,
                'format': '/sbin/mkfs.jfs',
                'formatparam': '{labelparam} -f {device}',
                'gpt': '8300',
                'label': '-L {labelname}',
                'msdos': '83',
                'ssd': ['discard'],
                'type': ['hdd', 'usb-hdd']},
        'xfs': {'defaultopt': defaultOpt,
                'format': '/sbin/mkfs.xfs',
                'formatparam': '{labelparam} -f {device}',
                'gpt': '8300',
                'label': '-L {labelname}',
                'msdos': '83',
                'ssd': ['discard'],
                'type': ['hdd', 'usb-hdd']},
#        'nilfs2': {'defaultopt': defaultOpt,
#                   'format': '/sbin/mkfs.nilfs2',
#                   'formatparam': '{labelparam} {device}',
#                   'gpt': '8300',
#                   'label': '-L {labelname}',
#                   'msdos': '83',
#                   'ssd': [],
#                   'type': ['hdd', 'usb-hdd']},
        'swap': {'defaultopt': ['sw'],
                 'format': '/sbin/mkswap',
                 'formatparam': '{device}',
                 'gpt': '8200',
                 'label': '',
                 'ssd': [],
                 'msdos': '82'},
        'uefi': {'defaultopt': defaultOpt,
                 'format': '/usr/sbin/mkfs.vfat',
                 'formatparam': '{labelparam} -F 32 {device}',
                 'gpt': 'EF00',
                 'label': '-n {labelname}',
                 'msdos': '0b',
                 'ssd': [],
                 'type': ['hdd']},
        'vfat': {'defaultopt': defaultOpt,
                 'format': '/usr/sbin/mkfs.vfat',
                 'formatparam': '{labelparam} -F 32 {device}',
                 'gpt': '0700',
                 'label': '-n {labelname}',
                 'msdos': '0b',
                 'ssd': ['discard'],
                 'type': ['flash']},
        'ntfs': {'defaultopt': defaultOpt,
                 'format': '/usr/sbin/mkfs.ntfs',
                 'formatparam': '{labelparam} -FQ {device}',
                 'gpt': '8300',
                 'label': '-L {labelname}',
                 'msdos': '7',
                 'ssd': [],
                 'compatible': ['ntfs-3g']},
        'ntfs-3g': {'defaultopt': defaultOpt,
                    'format': '/usr/sbin/mkfs.ntfs',
                    'formatparam': '{labelparam} -FQ {device}',
                    'gpt': '8300',
                    'label': '-L {labelname}',
                    'ssd': [],
                    'msdos': '7',
                    'compatible': ['ntfs']}}

    default_param = {'defaultopt': defaultOpt,
                     'ssd': []}

    @classmethod
    def firstAvailable(cls, listFS):
        for fs in listFS:
            if path.exists(cls.supportFS['format']):
                return fs
        else:
            return ""

    defaultFS = {
        'hdd': ("ext4"
                if path.exists(supportFS['ext4']['format']) else
                "reiserfs"
                if path.exists(supportFS['reiserfs']['format']) else
                "ext3"),
        'flash': "vfat",
        'usb-hdd': ("ext4"
                    if path.exists(supportFS['ext4']['format']) else
                    "reiserfs"
                    if path.exists(supportFS['reiserfs']['format']) else
                    "ext3")
    }

    @classmethod
    def getDefaultOpt(cls, fs, ssd=False):
        return ",".join(cls.supportFS.get(fs, cls.default_param)['defaultopt'] +
                        (cls.supportFS.get(fs, cls.default_param)['ssd']
                         if ssd else []))

    @classmethod
    def checkFSForTypeMount(cls, fs, roottype, mp):
        if mp == '/boot/efi':
            if fs not in ('uefi', 'vfat'):
                return False
            else:
                return True
        return roottype in cls.supportFS.get(fs, {}).get('type', [])
