/*
 * ModeSelectionView.java
 *
 * Copyright (c) 2016 Auth0 (http://auth0.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.auth0.android.lock.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.auth0.android.lock.R;
import com.auth0.android.lock.internal.configuration.AuthMode;

@SuppressLint("ViewConstructor")
public class ModeSelectionView extends LinearLayout implements TabLayout.OnTabSelectedListener {

    private final ModeSelectedListener callback;
    private TabLayout tabLayout;
    private View firstTabView;
    private View secondTabView;

    public ModeSelectionView(Context context, @NonNull ModeSelectedListener listener) {
        super(context);
        this.callback = listener;
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.com_auth0_lock_tab_layout, this);
        tabLayout = findViewById(R.id.com_auth0_lock_tab_layout);

        firstTabView = inflate(getContext(), R.layout.com_auth0_lock_tab, null);
        secondTabView = inflate(getContext(), R.layout.com_auth0_lock_tab, null);

        final TabLayout.Tab firstTab = tabLayout.newTab()
                .setCustomView(firstTabView)
                .setText(R.string.com_auth0_lock_mode_log_in);
        final TabLayout.Tab secondTab = tabLayout.newTab()
                .setCustomView(secondTabView)
                .setText(R.string.com_auth0_lock_mode_sign_up);

        firstTabView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelectedMode(AuthMode.LOG_IN);
            }
        });
        secondTabView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setSelectedMode(AuthMode.SIGN_UP);
            }
        });

        tabLayout.addTab(firstTab);
        tabLayout.addTab(secondTab);
    }

    public void setSelectedMode(@AuthMode int mode) {
        TabLayout.Tab tab = tabLayout.getTabAt(mode);
        tab.select();
        toggleBoldText(firstTabView, mode == AuthMode.LOG_IN);
        toggleBoldText(secondTabView, mode == AuthMode.SIGN_UP);
        callback.onModeSelected(mode);
    }

    private void toggleBoldText(View tabView, boolean bold) {
        final TextView text = tabView.findViewById(android.R.id.text1);
        text.setTypeface(bold ? text.getTypeface() : null, bold ? Typeface.BOLD : Typeface.NORMAL);
    }

    @Deprecated
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        //No-Op
    }

    @Deprecated
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        //No-Op
    }

    @Deprecated
    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        //No-Op
    }

    public interface ModeSelectedListener {
        void onModeSelected(@AuthMode int mode);

        @AuthMode
        int getSelectedMode();
    }
}

@@ -2200,28 +2200,36 @@
         
-firstTabView
+View tabDescription1
  = infla
@@ -2289,29 +2289,36 @@
         
-secondTabView
+View tabDescription2
  = infla
@@ -2460,28 +2460,31 @@
 tomView(
-firstTabView
+tabDescription1
 )%0A      
@@ -2630,29 +2630,31 @@
 tomView(
-secondTabView
+tabDescription2
 )%0A      
@@ -2710,16 +2710,135 @@
 ign_up);
+%0A        firstTabView = (View) tabDescription1.getParent();%0A        secondTabView = (View) tabDescription2.getParent();
 %0A%0A      
@@ -3873,32 +3873,16 @@
 %0A    %7D%0A%0A
-    @Deprecated%0A
     @Ove
@@ -3879,32 +3879,32 @@
 %0A%0A    @Override%0A
+
     public void 
@@ -3961,32 +3961,16 @@
 %0A    %7D%0A%0A
-    @Deprecated%0A
     @Ove
@@ -4055,24 +4055,8 @@
  %7D%0A%0A
-    @Deprecated%0A
     

fix tab sync issue by changing the on click listener