/*
 * Copyright (c) 2018 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * 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.
 */

package ffc.app

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import android.net.Uri
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.view.GravityCompat
import android.support.v7.app.ActionBarDrawerToggle
import android.transition.Fade
import android.view.MenuItem
import android.widget.ImageView
import android.widget.TextView
import ffc.android.enter
import ffc.android.load
import ffc.android.observe
import ffc.android.onClick
import ffc.android.sceneTransition
import ffc.android.setTransition
import ffc.android.viewModel
import ffc.app.auth.auth
import ffc.app.location.GeoMapsFragment
import ffc.app.location.housesOf
import ffc.app.search.SearchActivity
import ffc.app.setting.AboutActivity
import ffc.app.setting.SettingsActivity
import kotlinx.android.synthetic.main.activity_main.drawerLayout
import kotlinx.android.synthetic.main.activity_main.navView
import kotlinx.android.synthetic.main.activity_main_content.addLocationButton
import kotlinx.android.synthetic.main.activity_main_content.searchButton
import kotlinx.android.synthetic.main.activity_main_content.toolbar
import kotlinx.android.synthetic.main.activity_main_content.versionView
import org.jetbrains.anko.browse
import org.jetbrains.anko.dimen
import org.jetbrains.anko.find
import org.jetbrains.anko.intentFor
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class MainActivity : FamilyFolderActivity(), NavigationView.OnNavigationItemSelectedListener {

    private val geoMapsFragment by lazy { GeoMapsFragment() }

    private val viewModel by lazy { viewModel<MainViewModel>() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setTransition {
            exitTransition = null
            reenterTransition = Fade().enter()
        }

        searchButton.onClick {
            startActivity(intentFor<SearchActivity>(),
                sceneTransition(toolbar to getString(R.string.transition_appbar)))
        }

        setupNavigationDrawer()
        versionView.text = BuildConfig.VERSION_NAME

        with(geoMapsFragment) { setPaddingTop(dimen(R.dimen.maps_padding_top)) }
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.contentContainer, geoMapsFragment, "geomap")
            .commit()

        addLocationButton.hide()
        observe(viewModel.houseNoLocation) {
            if (it == true) addLocationButton.show() else addLocationButton.hide()
        }
    }

    private fun setupNavigationDrawer() {
        val toggle = ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close)
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()
        navView.setNavigationItemSelectedListener(this)
    }

    override fun onResume() {
        super.onResume()
        checkHouseNoLocation()

        with(navView.getHeaderView(0)) {
            val user = auth(context).user!!
            find<TextView>(R.id.userDisplayNameView).text = user.displayName
            user.avatarUrl?.let { find<ImageView>(R.id.userAvartarView).load(Uri.parse(it)) }
            find<TextView>(R.id.orgDisplayNameView).text = org!!.displayName
        }
    }

    override fun onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_main -> {
            }
            R.id.nav_follow -> browse("https://www.facebook.com/FFC.NECTEC/", true)
            R.id.nav_achivement, R.id.nav_manual -> {
                toast(R.string.under_construction)
            }
            R.id.nav_about -> startActivity<AboutActivity>()
            R.id.nav_settings -> startActivity<SettingsActivity>()
            R.id.nav_logout -> {
                auth(this).clear()
                finish()
            }
        }
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

    private fun checkHouseNoLocation() {
        housesOf(org!!).houseNoLocation {
            onFound { viewModel.houseNoLocation.value = true }
            onNotFound { viewModel.houseNoLocation.value = false }
            onFail { }
        }
    }

    class MainViewModel : ViewModel() {
        val houseNoLocation = MutableLiveData<Boolean>()
    }
}
