package com.meeting.demo.view.sdk;

import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;

import com.huawei.cloudlink.openapi.HWMSdk;
import com.huawei.hwmconf.presentation.view.activity.InMeetingActivity;
import com.huawei.hwmconf.sdk.model.conf.entity.HWMCustomSubViewModel;
import com.huawei.hwmconf.sdk.model.conf.entity.HWMCustomSubViewPosition;
import com.huawei.hwmfoundation.utils.route.HCActivityManager;
import com.meeting.demo.R;
import com.meeting.demo.model.CustomSubViewModel;
import com.meeting.demo.utils.CustomSubViewCache;
import com.meeting.demo.utils.DemoUtil;
import com.meeting.demo.view.base.BaseDialogFragment;

public class CustomSubViewConfigFragment extends BaseDialogFragment {

    private static final String TAG = "CustomSubViewConfig";

    private CustomSubViewCache cache = CustomSubViewCache.getInstance();
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_custom_subview_config, container, false);
        setupPositionControls(R.id.topLeftSection, cache.getCustomSubViewModelTopLeft());
        setupPositionControls(R.id.bottomCenterSection, cache.getCustomSubViewModelBottomCenter());
        setupPositionControls(R.id.bottomRightSection, cache.getCustomSubViewModelBottomRight());
        return rootView;
    }
    @Override
    public void onStart() {
        super.onStart();
        setDialogSize();
    }

    private void setDialogSize() {
        Dialog dialog = getDialog();
        if (dialog != null && dialog.getWindow() != null) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            requireActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int screenWidth = displayMetrics.widthPixels;
            int dialogWidth = (int) (screenWidth * 0.8);
            int dialogHeight = WindowManager.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().setLayout(dialogWidth, dialogHeight);
            dialog.getWindow().setGravity(Gravity.CENTER);
        }
    }

    private void setupPositionControls(int sectionId, CustomSubViewModel model) {
        View section = rootView.findViewById(sectionId);

        CheckBox cbOpen = section.findViewById(R.id.cb_open);
        CheckBox cbDrag = section.findViewById(R.id.cb_drag);
        CheckBox cbAttach = section.findViewById(R.id.cb_attach);
        CheckBox cbFollow = section.findViewById(R.id.cb_follow);
        RadioGroup rgAttachType = section.findViewById(R.id.rg_attach_type);

        cbOpen.setChecked(model.isOpen());
        cbDrag.setChecked(model.isEnableDrag());
        cbAttach.setChecked(model.isEnableAttach());
        cbFollow.setChecked(model.isShowFollowConfCtrlBar());

        if (model.getAttachType() == HWMCustomSubViewModel.HWMCustomSubViewAttachType.HWM_CUSTOM_SUBVIEW_ATTACH_TYPE_TOP_AND_BOTTOM) {
            rgAttachType.check(R.id.rb_vertical);
        } else {
            rgAttachType.check(R.id.rb_horizontal);
        }

        cbOpen.setOnCheckedChangeListener((v, checked) -> model.setOpen(checked));
        cbDrag.setOnCheckedChangeListener((v, checked) -> model.setEnableDrag(checked));
        cbAttach.setOnCheckedChangeListener((v, checked) -> model.setEnableAttach(checked));
        cbFollow.setOnCheckedChangeListener((v, checked) -> model.setShowFollowConfCtrlBar(checked));

        rgAttachType.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_vertical) {
                model.setAttachType(HWMCustomSubViewModel.HWMCustomSubViewAttachType.HWM_CUSTOM_SUBVIEW_ATTACH_TYPE_TOP_AND_BOTTOM);
            } else {
                model.setAttachType(HWMCustomSubViewModel.HWMCustomSubViewAttachType.HWM_CUSTOM_SUBVIEW_ATTACH_TYPE_LEFT_AND_RIGHT);
            }
        });

        Button button = section.findViewById(R.id.btn_refresh);
        boolean isInMeetingView = HCActivityManager.getInstance().getCurrentActivity() instanceof InMeetingActivity;
        if (isInMeetingView) {
            button.setVisibility(View.VISIBLE);
        } else {
            button.setVisibility(View.GONE);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateMeetingCustomSubView(model.getHwmCustomSubViewPosition());
            }
        });
    }


    private void updateMeetingCustomSubView(HWMCustomSubViewPosition customSubViewPosition) {
        HWMSdk.getOpenApi(DemoUtil.getApplication()).updateMeetingCustomSubView(customSubViewPosition);
        dismiss();
    }
}
