package com.fekracomputers.quran.UI.Activities;

import android.app.ActivityManager;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.Process;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.fekracomputers.quran.Adapter.TranslationAdapter;
import com.fekracomputers.quran.Database.AppPreference;
import com.fekracomputers.quran.Database.DatabaseAccess;
import com.fekracomputers.quran.Downloader.DownloadService;
import com.fekracomputers.quran.Downloader.DownloadTafseerService;
import com.fekracomputers.quran.Models.TranslationBook;
import com.fekracomputers.quran.R;
import com.fekracomputers.quran.Utilities.AppConstants;
import com.fekracomputers.quran.Utilities.QuranValidateSources;
import com.fekracomputers.quran.Utilities.Settingsss;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Activity for management download translations and explanation
 */
public class TranslationsActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView translationManagmentList;
    private TranslationAdapter adapter;
    private List<TranslationBook> listOfDownloaded, listToDownload;
    private Dialog progress;
    private ProgressBar downloadProgress;
    private TextView downloadInfo;
    int k = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //display back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//display back button
        getSupportActionBar().setTitle(getString(R.string.translations));//set action bar title
        setContentView(R.layout.activity_translation);
        init();
    }

    /**
     * Function to init activity view
     */
    private void init() {

        //init views
        adapter = new TranslationAdapter(this);
        translationManagmentList = (ListView) findViewById(R.id.download);
        translationManagmentList.setOnItemClickListener(this);
        translationManagmentList.setTextFilterEnabled(true);
        translationManagmentList.setEmptyView(findViewById(R.id.progressBar3));
        translationManagmentList.setAdapter(adapter);

        //async thread to load translations
        new TafaseerLists().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    private void showDownloadPop() {
        progress = new Dialog(this);
        progress.requestWindowFeature(Window.FEATURE_NO_TITLE);
        progress.setContentView(R.layout.download_popup);
        progress.setCancelable(false);
        downloadProgress = (ProgressBar) progress.findViewById(R.id.pb_download);
        downloadInfo = (TextView) progress.findViewById(R.id.tv_download_info);
        Button cancelDownload = (Button) progress.findViewById(R.id.b_cancel_download);

        cancelDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new TafaseerLists().execute();
                progress.cancel();
                stopService(new Intent(TranslationsActivity.this, DownloadTafseerService.class));
            }
        });

        progress.show();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(downloadBroadcast, new IntentFilter(AppConstants.Download.INTENT));

        //check if the download service run
        if (!Settingsss.isMyServiceRunning(TranslationsActivity.this, DownloadTafseerService.class) && progress != null) {
            progress.dismiss();
            new TafaseerLists().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else if (progress != null) {
            progress.show();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(downloadBroadcast);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(TranslationsActivity.this, DownloadTafseerService.class));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        //check to remove or download translation
        final TranslationBook translationBook = adapter.getItem(position);
        final File tafseer = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + getString(R.string.app_folder_path) + "/tafaseer");
        if (translationBook.isDownloaded) {
            //remove translation
            final AlertDialog.Builder alert = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
            alert.setCancelable(false);
            alert.setTitle(getString(R.string.Remove));
            alert.setMessage(getString(R.string.removeTranslationAlert));
            alert.setNegativeButton(getString(R.string.removeTranslationAlertNegative), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alert.setPositiveButton(getString(R.string.removeTranslationAlertPositive), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (new File(tafseer.getAbsolutePath() + "/tafseer" + translationBook.bookID + AppConstants.Extensions.SQLITE).delete()) {
                        Toast.makeText(TranslationsActivity.this, getString(R.string.translation_deleted), Toast.LENGTH_SHORT).show();
                    }
                    new TafaseerLists().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
            });
            alert.show();
        } else if (!Settingsss.isMyServiceRunning(TranslationsActivity.this, DownloadTafseerService.class)) {
            int internetStatus = Settingsss.checkInternetStatus(this);
            if (internetStatus <= 0) {
                android.support.v7.app.AlertDialog.Builder builder =
                        new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
                builder.setCancelable(false);
                builder.setTitle(getResources().getString(R.string.Alert));
                builder.setMessage(getResources().getString(R.string.no_internet_alert));
                builder.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
                builder.show();
            } else {
                //add dirs and download
                if (!tafseer.exists()) tafseer.mkdirs();
                translationBook.downloading = true;
                AppPreference.setDownloadType(AppConstants.Preferences.TAFSEER);
                AppPreference.setDownloadID(position);
                Intent downloadService = new Intent(this, DownloadTafseerService.class);
                downloadService.putExtra(AppConstants.Download.DOWNLOAD_LOCATION, Environment.getExternalStorageDirectory().getAbsolutePath() + getString(R.string.app_folder_path));
                downloadService.putExtra(AppConstants.Download.DOWNLOAD_URL, AppConstants.Paths.TAFSEER_LINK + translationBook.bookID + AppConstants.Extensions.ZIP);
                downloadService.putExtra(AppConstants.Download.TYPE , AppConstants.Preferences.TAFSEER);
                startService(downloadService);
                showDownloadPop();

                adapter.notifyDataSetChanged();
            }


        }
    }

    /**
     * Class to get all downloaded tafaseer and tafaseer to download
     */
    private class TafaseerLists extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {
            listOfDownloaded = new ArrayList<>();
            listToDownload = new DatabaseAccess().getAllTranslations();
            listOfDownloaded.clear();
            List<Integer> downloadedBookIDs = new QuranValidateSources().getDownloadedTransaltions();
            for (TranslationBook translationBook : listToDownload) {
                for (Integer bookID : downloadedBookIDs) {
                    if (bookID == translationBook.bookID) {
                        translationBook.isDownloaded = true;
                        listOfDownloaded.add(translationBook);
                    }
                }
            }
            for (TranslationBook translationBook : listOfDownloaded) {
                listToDownload.remove(translationBook);
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            adapter.clear();
            if (listOfDownloaded.size() != 0)
                adapter.add(new TranslationBook(-1, getString(R.string.downloaded), null, 0, false, false));
            adapter.addAll(listOfDownloaded);
            adapter.add(new TranslationBook(-1, getString(R.string.downloadavaliable), null, 0, false, false));
            adapter.addAll(listToDownload);
            adapter.notifyDataSetChanged();
        }

    }

    /**
     * Class receive Broadcast that download finished to refresh list
     */
    private BroadcastReceiver downloadBroadcast = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("CHECK_INTERNET_LOST" , "receive broad cast");
            //intent from sent broadcast
            float value = intent.getLongExtra(AppConstants.Download.NUMBER, 0);
            float max = intent.getLongExtra(AppConstants.Download.MAX, 0);
            int type = intent.getIntExtra(AppConstants.Download.TYPE ,-1);
            String status = intent.getStringExtra(AppConstants.Download.DOWNLOAD);

            //cases of download
            if (status != null && type == AppConstants.Preferences.TAFSEER) {
                if (downloadProgress == null)return;
                if (status.equals(AppConstants.Download.IN_DOWNLOAD)) {
                    downloadProgress.setMax((int) max);
                    downloadProgress.setProgress((int) value);

                    DecimalFormat df = new DecimalFormat("#.##");
                    String maxDownload = df.format((max / 1000000));
                    String currentDownload = df.format((value / 1000000));

                    downloadInfo.setText(maxDownload + " " + getString(R.string.mb) + " / " + currentDownload + " " + getString(R.string.mb) +" ("+(int)((value*100)/max)+"% )");
                } else if (status.equals(AppConstants.Download.FAILED)) {

                    Log.i("CHECK_INTERNET_LOST" , "in failed if");
                    downloadProgress.setMax(1);
                    downloadProgress.setProgress(1);
                    progress.dismiss();
                    new TafaseerLists().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                } else if (status.equals(AppConstants.Download.SUCCESS)) {
                    downloadProgress.setMax(1);
                    downloadProgress.setProgress(1);
                    progress.dismiss();
                    new TafaseerLists().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }else if(status.equals(AppConstants.Download.IN_EXTRACT)){
                    downloadProgress.setVisibility(View.GONE);
                    downloadInfo.setText(intent.getStringExtra(AppConstants.Download.FILES));
                }else if (status.equals(AppConstants.Download.UNZIP) && progress != null && progress.isShowing()){
                    progress.dismiss();
                    new TafaseerLists().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
            }
        }
    };

}