# Copyright 2015 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import json

from subiquitycore.controllers.identity import BaseIdentityController
from subiquitycore.utils import disable_first_boot_service, run_command, mark_firstboot_complete

from console_conf.ui.views import IdentityView, LoginView


class IdentityController(BaseIdentityController):
    identity_view = IdentityView

    def default(self):
        title = "Profile setup"
        excerpt = "Enter an email address from your account in the store."
        footer = ""
        self.ui.set_header(title, excerpt)
        self.ui.set_footer(footer, 40)
        self.ui.set_body(self.identity_view(self.model, self.signal, self.opts, self.loop))

    def identity_done(self, email):
        if not self.opts.dry_run:
            self.ui.frame.body.progress.set_text("Contacting store...")
            self.loop.draw_screen()
            result = run_command(["snap", "create-user", "--sudoer", "--json", email])
            self.ui.frame.body.progress.set_text("")
            if result['status'] != 0:
                self.ui.frame.body.error.set_text("Creating user failed:\n" + result['err'])
                return
            else:
                # mark ourselves complete
                mark_firstboot_complete()

                data = json.loads(result['output'])
                result = {
                    'realname': email,
                    'username': data['username'],
                    }
                self.model.add_user(result)
        else:
            result = {
                'realname': email,
                'username': email,
                }
            self.model.add_user(result)
        self.signal.emit_signal('identity:login')

    def login(self):
        title = "Configuration Complete"
        footer = "View configured user and device access methods"
        self.ui.set_header(title)
        self.ui.set_footer(footer)

        net_model = self.controllers['Network'].model
        net_model.probe_network()
        configured_ifaces = net_model.get_configured_interfaces()
        login_view = LoginView(self.opts,
                               self.model,
                               self.signal,
                               self.model.user,
                               configured_ifaces)

        self.ui.set_body(login_view)

    def login_done(self):
        if not self.opts.dry_run:
            # stop the console-conf services (this will kill the current process).
            disable_first_boot_service()

        self.signal.emit_signal('quit')

@@ -1337,77 +1337,1155 @@
 p))%0A
-%0A    def identity_do
+        if self.is_device_owned():%0A            # FIXME: should be email rather than realname here, but extrausers only has email%0A            self.signal.emit_signal('identity:done', self.model.user.realname)%0A%0A    def is_device_ow
 ne
+d
 (self
-, email):%0A        if not self.opts.dry_run
+):%0A        %22%22%22 Check if device is owned %22%22%22%0A%0A        # TODO: use proper snap APIs.%0A        with open('/var/lib/extrausers/passwd', 'r') as extrausers_fp:%0A            passwd_line = extrausers_fp.readline()%0A            if passwd_line and len(passwd_line) %3E 0:%0A                passwd = passwd_line.split(':')%0A                result = %7B%0A                    'realname': passwd%5B4%5D.split(',')%5B0%5D,%0A                    'username': passwd%5B0%5D,%0A                    %7D%0A                self.model.add_user(result)%0A                return True%0A        return False%0A%0A    def identity_done(self, email):%0A        if self.opts.dry_run:%0A            result = %7B%0A                'realname': email,%0A                'username': email,%0A                %7D%0A            self.model.add_user(result)%0A        elif self.is_device_owned():%0A            # user was added to the model, we don't need to do anything else.%0A            pass%0A        else
 :%0A  
@@ -3225,173 +3225,8 @@
 lt)%0A
-        else:%0A            result = %7B%0A                'realname': email,%0A                'username': email,%0A                %7D%0A            self.model.add_user(result)%0A
     

implement an initial, dumb "is this device owned" check.