package tw.com.altob.goodpk.Fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import java.util.List;

import tw.com.altob.goodpk.R;
import tw.com.altob.goodpk.Util.CommonIntent;
import tw.com.altob.goodpk.Util.PermissionCode;
import tw.com.altob.goodpk.Util.PermissionUtil;
import tw.com.altob.goodpk.Util.PrefsUtil;
import tw.com.altob.goodpk.Util.ScreenShot;

public class HomeFragment extends Fragment {


    private LinearLayout stationBtn;
    private TextView stationView;
    private ImageButton b1, b2, b3, b4;
    private ScreenShot mScreenShot;
    private final String[] stationTitle = {"市民廣場","新體","二重公園","金城","澳底"};
    private String[] stationCode = {"40808", "12143", "40806", "12117", "12145"};
    private String station;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.activity_home, container, false);

        this.b1 = (ImageButton) contentView.findViewById(R.id.imageButton);
        this.b2 = (ImageButton) contentView.findViewById(R.id.imageButton2);
        this.b3 = (ImageButton) contentView.findViewById(R.id.imageButton3);
        this.b4 = (ImageButton) contentView.findViewById(R.id.imageButton4);

        this.stationBtn = (LinearLayout) contentView.findViewById(R.id.stationBtn);
        this.stationView = (TextView) contentView.findViewById(R.id.stationView);

        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if(PrefsUtil.getFirst( getActivity())){
            showAlert();
        }else{
            PermissionUtil.requestResPermissions(HomeFragment.this, PermissionCode.READ_EXTERNAL_STORAGE);
        }

        this.b1.setTag(0);
        this.b2.setTag(1);
        this.b3.setTag(2);
        this.b4.setTag(3);

        this.b1.setOnClickListener( this.onBtnClickListener);
        this.b2.setOnClickListener( this.onBtnClickListener);
        this.b3.setOnClickListener( this.onBtnClickListener);
        this.b4.setOnClickListener( this.onBtnClickListener);
        this.stationBtn.setOnClickListener( this.stationLayoutOnClickListener);
    }

    private View.OnClickListener stationLayoutOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showListDialog();
        }
    };

    private void showListDialog(){
        AlertDialog.Builder dialog_list = new AlertDialog.Builder(getContext());
        dialog_list.setTitle( getString(R.string.action_station));
        dialog_list.setItems( stationTitle, new DialogInterface.OnClickListener(){
            @Override
            //只要你在onClick處理事件內，使用which參數，就可以知道按下陣列裡的哪一個了
            public void onClick(DialogInterface dialog, int which) {
                //Log.d("test", "onClick: " + which);
                stationView.setText(getText(R.string.action_station) + " : " + stationTitle[which]);
                station = stationCode[which];
            }
        });

        dialog_list.setNeutralButton( "取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
            }
        });

        dialog_list.show();
    }

    private Button.OnClickListener onBtnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            if(station == null){
                Toast.makeText(getContext(), "請先選擇場站", Toast.LENGTH_LONG).show();
            }else{
                switch ((int)v.getTag()){
                    case 0:
                        intent = CommonIntent.createInquireCarActivity(getActivity());
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.right_in,0);
                        break;
                    case 1:
                        intent = CommonIntent.createCarSecurityActivity(getActivity(), station);
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.right_in,0);
                        break;
                    case 2:
                        intent = CommonIntent.createHelpActivity(getActivity());
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.right_in,0);
                        break;
                    case 3:
                        intent = CommonIntent.createPaymentActivity(getActivity());
                        startActivity(intent);
                        getActivity().overridePendingTransition(R.anim.right_in,0);
                        break;
                }
            }
        }
    };

    private void showAlert(){
        AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity());
        dialog.setTitle( getString( R.string.main_alert_title));
        dialog.setMessage( getString( R.string.main_alert_content));
        dialog.setNeutralButton( getString(R.string.main_alert_btn2),new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                getActivity().finish();
            }

        });
        dialog.setNegativeButton( getString(R.string.main_alert_btn),new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                PrefsUtil.saveFirst( getActivity());
                PermissionUtil.requestResPermissions(HomeFragment.this, PermissionCode.READ_EXTERNAL_STORAGE);
            }

        });


        dialog.show();
    }

    //權限回調
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (requestCode == PermissionCode.READ_EXTERNAL_STORAGE) {
            if (PermissionUtil.checkResPermission(this.getActivity())) {
                //啟動截圖監聽
                mScreenShot = ScreenShot.getInstance();
                handelSystemScreenShot();
            } else{
                Toast.makeText( getContext(),getString(R.string.about_res_error), Toast.LENGTH_SHORT).show();
            }
        }
    }


    private void handelSystemScreenShot() {
        mScreenShot.register(getActivity(), new ScreenShot.CallbackListener() {
            @Override
            public void onShot(String path) {
                //Log.d("test", " shot : " + path);
                Toast.makeText( getContext(),getString(R.string.about_res), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
