package com.ttstd.dialer.fragment.dialog.shortcut;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.ttstd.dialer.R;
import com.ttstd.dialer.base.mvvm.fragment.BaseMvvmDialogFragment;
import com.ttstd.dialer.databinding.FragmentDialogShortcutBinding;
import com.ttstd.dialer.db.app.AppInfo;

public class ShortcutDialogFagment extends BaseMvvmDialogFragment<ShortcutViewModel, FragmentDialogShortcutBinding> {
    private static final String TAG = "ShortcutDialogFagment";

    private Activity mContext;
    private PackageManager mPackageManager;

    private AppInfo mAppInfo;
    private String mTitil;
    private String mTips;
    private String mNegativeText;
    private String mPositiveText;

    public ShortcutDialogFagment(AppInfo appInfo) {
        mAppInfo = appInfo;
    }

    public void setTitil(String titil) {
        mTitil = titil;
    }

    public void setTips(String tips) {
        mTips = tips;
    }

    public void setNegativeText(String negativeText) {
        mNegativeText = negativeText;
    }

    public void setPositiveText(String positiveText) {
        mPositiveText = positiveText;
    }

    public interface OnClickListener {
        void onPositiveClick();

        void onNegativeClick();
    }

    private OnClickListener mOnClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dialog_shortcut;
    }

    @Override
    protected void initDataBinding() {
        mContext = getActivity();
        mPackageManager = mContext.getPackageManager();
        mViewModel.setContext(mContext);
        mViewModel.setVDBinding(mViewDataBinding);
        mViewModel.setLifecycle(getLifecycleSubject());
        mViewDataBinding.setClick(new BtnClick());
    }

    @Override
    protected void initView(Bundle bundle) {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (TextUtils.isEmpty(mTitil)) {
            mViewDataBinding.tvTitle.setText("提示");
        } else {
            mViewDataBinding.tvTitle.setText(mTitil);
        }

        if (TextUtils.isEmpty(mTips)) {
            mViewDataBinding.tvMessage.setText("");
            mViewDataBinding.tvMessage.setVisibility(View.GONE);
        } else {
            mViewDataBinding.tvMessage.setText(mTips);
            mViewDataBinding.tvMessage.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(mNegativeText)){
            mViewDataBinding.tvNegative.setText("取消");
        }else {
            mViewDataBinding.tvNegative.setText(mNegativeText);
        }

        if (TextUtils.isEmpty(mPositiveText)){
            mViewDataBinding.tvPositive.setText("确定");
        }else {
            mViewDataBinding.tvPositive.setText(mPositiveText);
        }

        if (mAppInfo != null) {
            try {
                ActivityInfo info = mPackageManager.getActivityInfo(mAppInfo.getComponentName(), 0);
                mViewDataBinding.tvAppName.setText(info.loadLabel(mPackageManager));
                Drawable rawIcon = info.loadIcon(mPackageManager);
                mViewDataBinding.ivIcon.setImageDrawable(rawIcon);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getDialog() != null) {
            Window window = getDialog().getWindow();
            if (window == null) return;
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.gravity = Gravity.CENTER;
            window.setAttributes(params);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getDialog().setCancelable(true);
            getDialog().setCanceledOnTouchOutside(true);
        }
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        DialogFragment fragment = (DialogFragment) manager.findFragmentByTag(tag);
        if (fragment != null && fragment.isAdded()
                && fragment.getDialog() != null && fragment.getDialog().isShowing()) {
            return;
        }

        try {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        } catch (Exception e) {
            Log.e(TAG, "show: " + e.getMessage());
        }
    }

    @Override
    public void fetchData() {

    }

    public class BtnClick {
        public void onPositive(View view) {
            if (mOnClickListener != null) {
                mOnClickListener.onPositiveClick();
            }
        }

        public void onNegative(View view) {
            if (mOnClickListener != null) {
                mOnClickListener.onNegativeClick();
            }
        }

    }
}
