package com.application.tak.takapplication;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Movie;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.application.tak.takapplication.adapters.ViewPagerAdapter;
import com.application.tak.takapplication.data_access.Config;
import com.application.tak.takapplication.data_access.GetAllClientTasks;
import com.application.tak.takapplication.data_model.User;
import com.application.tak.takapplication.interfaces.OnDBRequestFinished;
import com.application.tak.takapplication.services.BackgroundService;
import com.application.tak.takapplication.services.LoginStatus;
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;

import java.util.ArrayList;
import java.util.List;

public class actClientTask extends AppCompatActivity {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    ViewPagerAdapter viewPagerAdapter;
    Menu menu;
    Context context;
    actClientTaskNotSelected _actClientTaskNotSelected;
    actClientTaskToDo _actClientTaskToDo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_client_task_copy);
        context = this;
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        _actClientTaskNotSelected = new actClientTaskNotSelected();
        _actClientTaskToDo = new actClientTaskToDo();

        viewPagerAdapter.addFragments(_actClientTaskNotSelected,"Oczekujące");
        viewPagerAdapter.addFragments(_actClientTaskToDo,"Wybrane");
        viewPagerAdapter.addFragments(new actClientAddNewTask(),"Dodaj zadanie");

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

                switch (position)
                {
                    case 0:
                        if(_actClientTaskNotSelected != null)
                            _actClientTaskNotSelected.GetTasks();
                        break;
                    case 1:
                        if(_actClientTaskToDo != null)
                            _actClientTaskToDo.GetTasks();
                        break;
                }

            }

            @Override
            public void onPageSelected(int position) {

                switch (position)
                {
                    case 0:
                        if(_actClientTaskNotSelected != null)
                            _actClientTaskNotSelected.GetTasks();
                        break;
                    case 1:
                        if(_actClientTaskToDo != null)
                            _actClientTaskToDo.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_client_overall, menu);
        return true;
    }

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

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout) {

            LoginStatus logStatus = new LoginStatus(this.getApplicationContext());
            logStatus.LogOut(this);
            return true;
        }
        else if (id == R.id.action_showinfo)
        {

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
            alertDialog.setTitle("Dane kontaktowe");
            alertDialog.setMessage("W razie jakichkolwiek problemów bądź wszelkie uwagi prosimy zgłaszać 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_clientProfile)
        {

            String msg = Config.LoggedInClient.get_FName() + " " + Config.LoggedInClient.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);
    }



}

