package com.light.prank.sound.app.allui.main;


import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.light.prank.sound.app.allui.custom.AcSave;
import com.light.prank.sound.app.allui.custom.FragmentCustom;
import com.light.prank.sound.app.allui.like.FragmentLike;
import com.light.prank.sound.app.utils.MyUtils;
import com.prank.tool.sounds.app.R;
import com.prank.tool.sounds.app.databinding.AcMainBinding;

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

public class AcMain extends AppCompatActivity implements View.OnClickListener {

    private AcMainBinding activityMainBinding;

    private List<Fragment> fragmentList = new ArrayList<>();
    private FragmentLike fragmentLike;
    private FragmentCustom fragmentRecorder;
    private ActivityResultLauncher<String> audioPickerLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = AcMainBinding.inflate(getLayoutInflater());
        setContentView(activityMainBinding.getRoot());
        MyUtils.setStatusBarTextColor(this, true);
        initVp2();
        activityMainBinding.frameHome.setOnClickListener(this);
        activityMainBinding.frameAdd.setOnClickListener(this);
        activityMainBinding.frameCollect.setOnClickListener(this);
        audioPickerLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(),
                new ActivityResultCallback<Uri>() {
                    @Override
                    public void onActivityResult(Uri uri) {
                        // Handle the returned Uri
                        if (uri != null) {
                            long duration = MyUtils.getAudioDuration(AcMain.this, uri);
                            Intent intent = new Intent(AcMain.this, AcSave.class);
                            intent.putExtra(AcSave.KEY_Path, uri.toString());
                            intent.putExtra(AcSave.KEY_Time, duration);
                            startActivity(intent);
                        }
                    }
                });


    }

    private void initVp2() {
        fragmentLike = FragmentLike.newInstance();
        fragmentRecorder = FragmentCustom.newInstance();

        fragmentList.add(FragmentMain.newInstance());
        fragmentList.add(fragmentRecorder);
        fragmentList.add(fragmentLike);
        activityMainBinding.vp2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }
        });

        activityMainBinding.vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        activityMainBinding.vp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                setCurSelectTab(position);
            }
        });

    }


    @Override
    protected void onResume() {
        super.onResume();
        int currentItem = activityMainBinding.vp2.getCurrentItem();
        if (currentItem == 1) {
            fragmentRecorder.refresh();
        } else if (currentItem == 2) {
            fragmentLike.refresh();
        }

    }

    private void setCurSelectTab(int position) {
        if (position == 0) {
            activityMainBinding.frameHome.setSelected(true);
            activityMainBinding.frameAdd.setSelected(false);
            activityMainBinding.frameCollect.setSelected(false);
            activityMainBinding.imHome.setSelected(true);
            activityMainBinding.imAdd.setSelected(false);
            activityMainBinding.imCollect.setSelected(false);
//            activityMainBinding.imImport.setVisibility(View.GONE);
        } else if (position == 1) {
            activityMainBinding.frameHome.setSelected(false);
            activityMainBinding.frameAdd.setSelected(true);
            activityMainBinding.frameCollect.setSelected(false);

            activityMainBinding.imHome.setSelected(false);
            activityMainBinding.imAdd.setSelected(true);
            activityMainBinding.imCollect.setSelected(false);
//            activityMainBinding.imImport.setVisibility(View.VISIBLE);
            fragmentRecorder.refresh();
        } else {
            activityMainBinding.frameHome.setSelected(false);
            activityMainBinding.frameAdd.setSelected(false);
            activityMainBinding.frameCollect.setSelected(true);

            activityMainBinding.imHome.setSelected(false);
            activityMainBinding.imAdd.setSelected(false);
            activityMainBinding.imCollect.setSelected(true);
//            activityMainBinding.imImport.setVisibility(View.GONE);
            fragmentLike.refresh();
        }

    }


    @Override
    public void onClick(View v) {
        if (v.equals(activityMainBinding.frameHome)) {
            activityMainBinding.vp2.setCurrentItem(0);
        } else if (v.equals(activityMainBinding.frameAdd)) {
            activityMainBinding.vp2.setCurrentItem(1);
        } else if (v.equals(activityMainBinding.frameCollect)) {
            activityMainBinding.vp2.setCurrentItem(2);
        }
//        else if (v.equals(activityMainBinding.imImport)) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
//                if (ContextCompat.checkSelfPermission(this, ConstValues.permission_read_audio[0])
//                        != PackageManager.PERMISSION_GRANTED) {
//                    ActivityCompat.requestPermissions(this, ConstValues.permission_read_audio,
//                            ConstValues.REQUEST_READ_STORAGE_PERMISSION);
//                } else {
//                    importAudio();
//                }
//            } else {
//                if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                        != PackageManager.PERMISSION_GRANTED ||
//                        ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
//                                != PackageManager.PERMISSION_GRANTED) {
//
//                    ActivityCompat.requestPermissions(this, ConstValues.permission_store, ConstValues.REQUEST_READ_STORAGE_PERMISSION);
//                } else {
//                    importAudio();
//                }
//            }
//        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MyUtils.REQUEST_READ_STORAGE_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                importAudio();
            } else {
                Toast.makeText(this, getString(R.string.permission_fail), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void importAudio() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/*");
        audioPickerLauncher.launch("audio/*");
    }

}