package com.application.tak.takapplication;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.application.tak.takapplication.adapters.ViewPagerAdapter;
import com.application.tak.takapplication.data_access.Config;
import com.application.tak.takapplication.services.LoginStatus;
import com.application.tak.takapplication.student.task.actStudentAllTask;
import com.application.tak.takapplication.student.task.actStudentMyTask;
import com.application.tak.takapplication.student.task.actStudentTaskPlan;
import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.login.LoginManager;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;

public class actStudentTask extends AppCompatActivity {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;
    GoogleApiClient mGoogleApiClient;
    Context context;
    actStudentTaskPlan _actStudentTaskPlan;
    actStudentAllTask _actStudentAllTask;
    actStudentMyTask _actStudentMyTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_student_task);
        context = this;
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
        mGoogleApiClient.connect();

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        _actStudentTaskPlan = new actStudentTaskPlan();
        _actStudentAllTask = new actStudentAllTask();
        _actStudentMyTask = new actStudentMyTask();

        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.addFragments(_actStudentTaskPlan,"Zaplanowane");
        viewPagerAdapter.addFragments(_actStudentAllTask,"Wszystkie");
        viewPagerAdapter.addFragments(_actStudentMyTask,"Moje zadania");

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                switch (position)
                {
                    case 0:
                        if(_actStudentTaskPlan != null)
                            _actStudentTaskPlan.GetTasks();
                        break;
                    case 1:
                        if(_actStudentAllTask != null)
                            _actStudentAllTask.GetTasks();
                        break;
                    case 2:
                        if(_actStudentMyTask != null)
                            _actStudentMyTask.GetTasks();
                        break;
                }

            }

            @Override
            public void onPageSelected(int position) {

                switch (position)
                {
                    case 0:
                        if(_actStudentTaskPlan != null)
                            _actStudentTaskPlan.GetTasks();
                        break;
                    case 1:
                        if(_actStudentAllTask != null)
                            _actStudentAllTask.GetTasks();
                        break;
                    case 2:
                        if(_actStudentMyTask != null)
                            _actStudentMyTask.GetTasks();
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_student_overall, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout) {
            LogOutOfGoogle();
            LogOutOfFacebook();
            LoginStatus logStatus = new LoginStatus(this.getApplicationContext());
            logStatus.LogOut(this);
            return true;
        }
        else if (id == R.id.action_problemstudent)
        {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
            alertDialog.setTitle("Dane kontaktowe");
            alertDialog.setMessage("W razie jakichkolwiek problemów bądź wszelkie uwagi prosimy kierować za pomocą wiadomości e-mail na adres: \n" +
                    "lepszyswiatfundacja@gmail.com\n");
            alertDialog.setIcon(R.drawable.email2);
            alertDialog.setPositiveButton("Zamknij",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int which) {

                            dialog.cancel();

                        }
                    });
            alertDialog.show();
        }
        else if (id == R.id.action_infostudent)
        {
            String msg = Config.LoggedInStudent.get_FName() + " " + Config.LoggedInStudent.get_LName();

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
            alertDialog.setTitle("Zalogowano jako:");
            alertDialog.setMessage(msg);
            alertDialog.setIcon(R.drawable.user);
            alertDialog.setPositiveButton("Zamknij",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int which) {

                            dialog.cancel();

                        }
                    });
            alertDialog.show();
        }


        return super.onOptionsItemSelected(item);
    }

    void LogOutOfGoogle()
    {
        Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {

                    }
                });
    }

    void LogOutOfFacebook()
    {
        if (AccessToken.getCurrentAccessToken() == null) {
            return; // already logged out
        }

        new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse graphResponse) {

                LoginManager.getInstance().logOut();

            }
        }).executeAsync();
    }
}

