package com.sendsms.activity;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

import com.sendsms.R;
import com.sendsms.delegate.NavigationDeleget;
import com.sendsms.fragment.FragmentDashboard;
import com.sendsms.fragment.FragmentDetails;
import com.sendsms.utills.Constant;

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

import static android.os.Build.VERSION_CODES.M;

/**
 * Created by hemants on 6/29/2016.
 */
public class DeshboardActivity extends FragmentActivity implements NavigationDeleget{

    Context mContext;
    Fragment mFragment = null;
    public FragmentManager mFragmentManager = null;
    FragmentTransaction fragmentTransaction = null;
    public NavigationDeleget mNavigationDeleget;
    final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;

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

        mContext = this;
        mNavigationDeleget = (NavigationDeleget) this;
        mFragmentManager = getSupportFragmentManager();

        setContentView(R.layout.activity_deshboard);

        if (Build.VERSION.SDK_INT >= M) {
            insertDummyContactWrapper();
        } else {
        }
        initializeView();
//        if(checkWriteExternalPermission()){
//            initializeView();
//    }

    }


    public void initializeView() {

        displayView(Constant.FRAGMENT_DASHBOARD, null);
    }

    @Override
    public void executeFragment(String fragmentName, Object obj) {
        displayView(fragmentName,obj);
    }

    @Override
    public void goBack() {
        super.onBackPressed();
    }


    private void displayView(String fragmentName, Object obj) {

        fragmentTransaction = mFragmentManager.beginTransaction();

        if (fragmentName.equals(Constant.FRAGMENT_DASHBOARD)) {
            mFragment = new FragmentDashboard(mContext);
        }
        else if (fragmentName.equals(Constant.FRAGMENT_DETAILS)) {
            mFragment = new FragmentDetails(mContext);
        }


        if (mFragment != null) {
            try {
                if (fragmentName.equals(Constant.FRAGMENT_DASHBOARD)) {
                    fragmentTransaction.replace(R.id.fragment_activity, mFragment);
                    fragmentTransaction.commit();
                }
                else if (fragmentName.equals(Constant.FRAGMENT_DETAILS)) {
                    fragmentTransaction.replace(R.id.fragment_activity, mFragment).addToBackStack(fragmentName);
                    fragmentTransaction.commit();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private boolean checkWriteExternalPermission()
    {
        String permission = "android.permission.READ_PHONE_STATE";
        int res = checkCallingOrSelfPermission(permission);
        return (res == PackageManager.PERMISSION_GRANTED);
    }

    @TargetApi(23)
    private void insertDummyContactWrapper() {
        List<String> permissionsNeeded = new ArrayList<String>();

        final List<String> permissionsList = new ArrayList<String>();
        if (!addPermission(permissionsList, android.Manifest.permission.ACCESS_NETWORK_STATE))
            permissionsNeeded.add("ACCESS NETWORK STATE");
        if (!addPermission(permissionsList, android.Manifest.permission.GET_ACCOUNTS))
            permissionsNeeded.add("GET ACCOUNTS");
        if (!addPermission(permissionsList, android.Manifest.permission.SEND_SMS))
            permissionsNeeded.add("SEND SMS");


        if (permissionsList.size() > 0) {
            if (permissionsNeeded.size() > 0) {
                // Need Rationale
                String message = "You need to grant access to " + permissionsNeeded.get(0);
                for (int i = 1; i < permissionsNeeded.size(); i++)
                    message = message + ", " + permissionsNeeded.get(i);
                showMessageOKCancel(message,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                                        REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                            }
                        });
                return;
            }
            requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
            return;
        }


    }

    @TargetApi(23)
    private boolean addPermission(List<String> permissionsList, String permission) {
        if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
            permissionsList.add(permission);
            // Check for Rationale Option
            if (!shouldShowRequestPermissionRationale(permission))
                return false;
        }
        return true;
    }

    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(DeshboardActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }
}
