package de.sebse.fuplanner.fragments.moddetails;


import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.io.File;
import java.util.Arrays;

import de.sebse.fuplanner.MainActivity;
import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.KVV.KVV;
import de.sebse.fuplanner.services.KVV.types.Modules;
import de.sebse.fuplanner.services.KVV.types.Resource;
import de.sebse.fuplanner.tools.UtilsDate;
import de.sebse.fuplanner.tools.logging.Logger;
import de.sebse.fuplanner.tools.ui.treeview.DirectoryNodeBinder;
import de.sebse.fuplanner.tools.ui.treeview.FileNodeBinder;
import de.sebse.fuplanner.tools.ui.treeview.TreeNode;
import de.sebse.fuplanner.tools.ui.treeview.TreeViewAdapter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ModDetailResourceFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ModDetailResourceFragment extends Fragment {
    private static final String ARG_POSITION = "itemPosition";

    private String mItemPos;
    private final Logger log = new Logger(this);
    private ModDetailResourceAdapter adapter;
    private SwipeRefreshLayout swipeLayout;


    public ModDetailResourceFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param itemPosition Item position in module list.
     * @return A new instance of fragment ModDetailAnnounceFragment.
     */
    public static ModDetailResourceFragment newInstance(String itemPosition) {
        ModDetailResourceFragment fragment = new ModDetailResourceFragment();
        Bundle args = new Bundle();
        args.putString(ARG_POSITION, itemPosition);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mItemPos = getArguments().getString(ARG_POSITION);
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recycler_view, container, false);
        // Set the adapter
        Context context = view.getContext();
        RecyclerView recyclerView = view.findViewById(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        adapter = new ModDetailResourceAdapter(Arrays.asList(new FileNodeBinder(), new DirectoryNodeBinder()));
        adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() {
            @Override
            public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) {
                if (!node.isLeaf()) {
                    //Update and toggle the node.
                    onToggle(!node.isExpand(), holder);
//                    if (!node.isExpand())
//                        adapter.collapseBrotherNode(node);
                } else {
                    KVV kvv = ((MainActivity) getActivity()).getKVV();
                    kvv.getModule(mItemPos, (Modules.Module module) -> {
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
                        Resource.File file = (Resource.File) node.getContent();
                        String ordnername = "FU-"+module.title;
                        File f = new File(Environment.getExternalStoragePublicDirectory(
                                Environment.DIRECTORY_DOWNLOADS)+"/"+ordnername+"/"+file.getTitle());
                        if (f.exists()) {//prüffen ob bereits herunter geladen
                            alertDialogBuilder
                                    .setTitle(file.getTitle())
                                    .setMessage(
                                            getResources().getString(R.string.creator_name, file.getAuthor()) + "\n" +
                                                    getResources().getString(R.string.last_modified_on, UtilsDate.getModifiedDateTime(context, file.getModifiedDate()))
                                    )
                                    .setCancelable(true)
                                    .setNeutralButton(R.string.close, (dialog, id) -> dialog.cancel())
                                    .setNegativeButton(R.string.openFile, (dialog, id) -> download(file, ordnername, false))
                                    .setPositiveButton(R.string.download, (dialog, id) -> download(file, ordnername, true));
                            AlertDialog alertDialog = alertDialogBuilder.create();
                            alertDialog.show();
                        }
                        else {
                            alertDialogBuilder
                                    .setTitle(file.getTitle())
                                    .setMessage(
                                            getResources().getString(R.string.creator_name, file.getAuthor()) + "\n" +
                                                    getResources().getString(R.string.last_modified_on, UtilsDate.getModifiedDateTime(context, file.getModifiedDate()))
                                    )
                                    .setCancelable(true)
                                    .setNeutralButton(R.string.close, (dialog, id) -> dialog.cancel())
                                    .setPositiveButton(R.string.download, (dialog, id) -> download(file, ordnername, true));
                            AlertDialog alertDialog = alertDialogBuilder.create();
                            alertDialog.show();
                        }

                    }, error -> {
                        log.e(error);
                    });
                }
                return false;
            }

            @Override
            public void onToggle(boolean isExpand, RecyclerView.ViewHolder holder) {
                DirectoryNodeBinder.ViewHolder dirViewHolder = (DirectoryNodeBinder.ViewHolder) holder;
                final ImageView ivArrow = dirViewHolder.getIvArrow();
                int rotateDegree = isExpand ? 90 : -90;
                ivArrow.animate().rotationBy(rotateDegree)
                        .start();
            }
        });

        recyclerView.setAdapter(adapter);

        // Getting SwipeContainerLayout
        swipeLayout = view.findViewById(R.id.swipe_container);
        // Adding Listener
        swipeLayout.setOnRefreshListener(() -> refresh(true));
        refresh(false);

        return view;
    }

    private void download(Resource.File file, String modul, boolean downloadNew){
        KVV kvv = ((MainActivity) getActivity()).getKVV();

            //log.d("ResourceFile berchtigungen: "+ isExternalStorageWritable());
            ActivityCompat.requestPermissions(ModDetailResourceFragment.super.getActivity(),
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);

            if(isExternalStorageWritable()){
                kvv.getResourceFile(success1 -> {
                    if (success1.equals("")){//Datei wurde nicht heruntergeladen
                        log.d("ResourceFile Datei nicht vorhanden");
                        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
                        alertDialogBuilder
                                .setTitle(R.string.ErrorFileDownload)
                                .setMessage(
                                        R.string.ErrorFileDownloadText
                                )
                                .setCancelable(true)
                                .setNeutralButton(R.string.close, (dialog, id) -> dialog.cancel());
                        AlertDialog alertDialog = alertDialogBuilder.create();
                        alertDialog.show();
                    }else {
                        fileOpen(new File(success1));
                    }
                }, error -> {

                    log.e(error);
                }, file.getTitle(), file.getUrl(), modul,  downloadNew );
            }







    }

    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        //log.d("ResourceFile Environment.getExternalStorageState()"+state);
        //log.d("ResourceFile Environment.MEDIA_MOUNTED"+Environment.MEDIA_MOUNTED);
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        log.d("Dateisystem: kein schreiben möglich");
        return false;
    }

    private void fileOpen(File url){
        Uri uri = Uri.fromFile(url);

        Intent intent = new Intent();//Intent.ACTION_VIEW
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type,
        // so Android knew what application to use to open the file
        if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
            // Word document
            intent.setDataAndType(uri, "application/msword");
        } else if(url.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf");
        } else if(url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
            // Powerpoint file
            intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        } else if(url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
            // Excel file
            intent.setDataAndType(uri, "application/vnd.ms-excel");
        } else if(url.toString().contains(".zip") || url.toString().contains(".rar")) {
            // WAV audio file
            intent.setDataAndType(uri, "application/x-wav");
        } else if(url.toString().contains(".rtf")) {
            // RTF file
            intent.setDataAndType(uri, "application/rtf");
        } else if(url.toString().contains(".wav") || url.toString().contains(".mp3")) {
            // WAV audio file
            intent.setDataAndType(uri, "audio/x-wav");
        } else if(url.toString().contains(".gif")) {
            // GIF file
            intent.setDataAndType(uri, "image/gif");
        } else if(url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) {
            // JPG file
            intent.setDataAndType(uri, "image/jpeg");
        } else if(url.toString().contains(".txt")) {
            // Text file
            intent.setDataAndType(uri, "text/plain");
        } else if(url.toString().contains(".3gp") || url.toString().contains(".mpg") || url.toString().contains(".mpeg") || url.toString().contains(".mpe") || url.toString().contains(".mp4") || url.toString().contains(".avi")) {
            // Video files
            intent.setDataAndType(uri, "video/*");
        } else {
            //if you want you can also define the intent type for any other file

            //additionally use else clause below, to manage other unknown extensions
            //in this case, Android will show all applications installed on the device
            //so you can choose which application to use
            intent.setDataAndType(uri, "*/*");
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }

    private void refresh(boolean forceRefresh) {
        if (getActivity() != null) {
            KVV kvv = ((MainActivity) getActivity()).getKVV();
            kvv.getModule(mItemPos, (Modules.Module module) -> {
                adapter.setModule(module);
                kvv.getModuleResources(module, success1 -> {
                    adapter.setModule();
                    swipeLayout.setRefreshing(false);
                }, error -> {
                    swipeLayout.setRefreshing(false);
                    log.e(error);
                }, forceRefresh);
            }, error -> {
                swipeLayout.setRefreshing(false);
                log.e(error);
            }, forceRefresh);
        }
    }

}
