package org.deke.risk.riskahead;

import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.util.Log;

import org.deke.risk.riskahead.helper.AppConfig;
import org.deke.risk.riskahead.helper.BaseActivity;
import org.deke.risk.riskahead.adapter.ProfilePagerAdapter;
import org.deke.risk.riskahead.helper.SessionManager;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class ProfileActivity extends BaseActivity{

    private final static String TAG = ProfileActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userHasToBeLoggedIn();

        Intent intent = getIntent();
        String msg_intent = intent.getStringExtra(AppConfig.EXTRA_MESSAGE);

        if(msg_intent != null){
            initProfileFromUserID(msg_intent);
        }else{
            initOwnProfileView();
        }

        Log.d(TAG,"ProfileActivity created");
    }

    private void initProfileFromUserID(String userId) {
        TabLayout tabLayout = findViewById(R.id.tab_layout);

        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.lbl_profile_stats)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.lbl_profile_reports)));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        initViewPager(tabLayout, userId);
    }

    private void initOwnProfileView() {
        TabLayout tabLayout = findViewById(R.id.tab_layout);

        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.lbl_profile_stats)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.lbl_profile_top10)));
        tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.lbl_profile_reports)));

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        initViewPager(tabLayout, user.get(SessionManager.KEY_UID));
    }

    private void initViewPager(TabLayout tabLayout, String userId) {
        final ViewPager viewPager = findViewById(R.id.pager);
        final PagerAdapter adapter = new ProfilePagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), userId);

        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        navDrawer.setSelection(userstatsAvtivityID, false);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_profile;
    }

    @Override
    protected String getActivityName() {
        return getResources().getString(R.string.title_activity_user_config);
    }

}
