package com.uiui.os.fragment;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.app.NavUtils;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uiui.os.R;
import com.uiui.os.utils.ApkUtils;
import com.uiui.os.utils.BitmapUtils;
import com.uiui.os.utils.IconUtils;
import com.uiui.os.view.MyGridLayout;

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

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AppListFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AppListFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ArrayList<ApplicationInfo> applicationInfos;
    private MyGridLayout gridLayout;
    private String TAG = AppListFragment.class.getSimpleName();

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AppListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AppListFragment newInstance(String param1, String param2) {
        AppListFragment fragment = new AppListFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            applicationInfos = savedInstanceState.getParcelableArrayList("applicationInfos");
        }
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_applist, container, false);
        initView();
        return rootView;
    }

    private void initView() {
        if (applicationInfos.size() != 12) {
            applicationInfos.addAll(new ArrayList<>(Arrays.asList(new ApplicationInfo[12 - applicationInfos.size()])));
        }
        gridLayout = rootView.findViewById(R.id.list);
        if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            gridLayout.set(4, 3);
        } else {
            gridLayout.set(3, 4);
        }

        gridLayout.setGridAdapter(new MyGridLayout.GridAdatper() {
            @Override
            public View getView(int index) {
                PackageManager pm = rootView.getContext().getPackageManager();
                View view = getLayoutInflater().inflate(R.layout.actions_item,
                        null);
                ImageView iv = view.findViewById(R.id.iv);
                TextView tv = view.findViewById(R.id.tv);
                LinearLayout linearLayout = view.findViewById(R.id.btn_booktag);
                ApplicationInfo applicationInfo = applicationInfos.get(index);

                if (applicationInfo != null) {
                    Log.e(TAG, "getView: " + applicationInfo.packageName);
                    int i = IconUtils.appClassNameList.indexOf(applicationInfo.packageName);
                    if (i != -1) {
                        String val = IconUtils.appIconList.get(i);
                        int resID = getActivity().getResources().getIdentifier(val, "drawable", "com.uiui.os");
                        if (resID == 0) {
                            iv.setImageDrawable(applicationInfo.loadIcon(pm));
                        } else {
                            iv.setImageDrawable(getActivity().getResources().getDrawable(resID));
                        }
                    } else {
                        iv.setImageBitmap(BitmapUtils.getIconBitmap(rootView.getContext(), applicationInfo.loadIcon(pm)));
                    }
                    tv.setText(applicationInfo.loadLabel(pm));
//                    linearLayout.setEnabled(true);
                } else {
//                    linearLayout.setEnabled(false);
                }

                return view;
            }

            @Override
            public int getCount() {
//                return applicationInfos == null ? 0 : applicationInfos.size();
                return applicationInfos.size();
            }
        });
        gridLayout.setApplicationInfos(applicationInfos);
        gridLayout.setOnItemClickListener(new MyGridLayout.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int index) {
                ApplicationInfo applicationInfo = applicationInfos.get(index);
                if (applicationInfo != null) {
                    ApkUtils.openPackage(v.getContext(), applicationInfo.packageName);
                }
            }
        });
    }

    public void setAppList(ArrayList<ApplicationInfo> appList) {
        this.applicationInfos = appList;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putParcelableArrayList("applicationInfos", applicationInfos);
        super.onSaveInstanceState(outState);
    }
}
